Mybatis数据的增删改查
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- <properties resource="jdbc.properties"/> --><properties><property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/><property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis"/><property name="jdbc.username" value="root"/><property name="jdbc.password" value="123456"/></properties><!-- <typeAliases><typeAlias alias="Student" type="com.qinb.model.Student"/></typeAliases> --><typeAliases><package name="com.qinb.model"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource></environment><environment id="test"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource></environment></environments><mappers><!-- <mapper resource="com/qinb/mappers/StudentMapper.xml" /> --><!-- <mapper class="com.qinb.mappers.StudentMapper"/> --><package name="com.qinb.mappers"/></mappers></configuration>
package com.qinb.util;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class SqlSessionFactoryUtil {private static SqlSessionFactory sqlSessionFactory;public static SqlSessionFactory getSqlSessionFactory(){if(sqlSessionFactory==null){InputStream inputStream=null;try{inputStream=Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);}catch(Exception e){e.printStackTrace();}}return sqlSessionFactory;}public static SqlSession openSession(){return getSqlSessionFactory().openSession();}}
package com.qinb.mappers;import java.util.List;import com.qinb.model.Student;public interface StudentMapper {public int add(Student student);public int update(Student student);public int delete(Integer id);public Student findById(Integer id);public List<Student> list();}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qinb.mappers.StudentMapper"><resultMap type="Student" id="StudentResult"><id property="id" column="id"/><result property="name" column="name"/><result property="age" column="age"/></resultMap><insert id="add" parameterType="Student" >insert into t_student values(null,#{name},#{age})</insert><update id="update" parameterType="Student" >update t_student set name=#{name},age=#{age} where id=#{id}</update><delete id="delete" parameterType="Integer">delete from t_student where id=#{id}</delete><select id="findById" parameterType="Integer" resultType="Student">select * from t_student where id=#{id}</select><select id="list" resultMap="StudentResult">select * from t_student</select></mapper>
package com.qinb.service;import static org.junit.Assert.fail;import java.util.List;import java.util.logging.Logger;import org.apache.ibatis.session.SqlSession;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.qinb.mappers.StudentMapper;import com.qinb.model.Student;import com.qinb.util.SqlSessionFactoryUtil;public class StudentTest2 {private static Logger logger=Logger.getLogger(StudentTest2.class.getName());private SqlSession sqlSession=null;private StudentMapper studentMapper= null;@Beforepublic void setUp() throws Exception {sqlSession = SqlSessionFactoryUtil.openSession();studentMapper=sqlSession.getMapper(StudentMapper.class);}@Afterpublic void tearDown() throws Exception {sqlSession.close();}@Testpublic void testAdd(){logger.info("添加学生");Student student = new Student("王五",20);studentMapper.add(student);sqlSession.commit();}@Testpublic void testUpdate(){logger.info("更新学生");Student student = new Student(5,"秦豹2",22);studentMapper.update(student);sqlSession.commit();}@Testpublic void testDelete(){logger.info("删除学生");studentMapper.delete(2);sqlSession.commit();}@Testpublic void testFindById(){logger.info("根据id获取学生");Student student =studentMapper.findById(5);System.out.println(student);sqlSession.commit();//查询可以不用提交事物}@Testpublic void testList(){logger.info("获取所有学生");List<Student> studentList = studentMapper.list();for(Student stu:studentList){System.out.println(stu);}}@Testpublic void test() {fail("Not yet implemented");}}
Mybatis数据的增删改查的更多相关文章
- Mybatis框架基于注解的方式,实对数据现增删改查
编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...
- Mybatis学习总结(二)—使用接口实现数据的增删改查
在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...
- Mybatis实现数据的增删改查
Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...
- dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)
jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...
- MVC模式:实现数据库中数据的增删改查功能
*.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...
- Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查
5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- 数据的增删改查(三层)<!--待补充-->
进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)” 既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充) 注: ...
- vue实现对表格数据的增删改查
在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...
随机推荐
- 007PHP基础知识——类型转换 外部变量
<?php /**类型转换 */ /*1.自由转换*/ /*2.强制转换:不改变原变量,生成新的变量*/ //转换为字符串: /*$a=100; $b=(string)$a; var_dump( ...
- Zabbix在CentOS7上的安装方法:
).zabbix-server 要安装zabbix-server首先需要安装MySQL数据库,当然你可以将MySQL换成其他的数据库 1.1)创建zabbix数据库:CREATE DATABASE z ...
- hdu 6040 Hints of sd0061(stl: nth_element(arr,arr+k,arr+n))
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- javascript 事件委托 event delegation
事件委托 event delegation 一.概念: 假设我们有很多个子元素,每个元素被点击时都会触发相应事件,普通的做法是给每个子元素添加一个事件监听. 而,事件委托则是给它们的父元素添加一个事件 ...
- New Concept English Two 34 game over
$课文95 纯属虚构 1049. When the Ambassador or Escalopia returned home for lunch, his wife got a shock. 当艾 ...
- LINUX系统下APACHE中的CGI应用
该实验环境是在APACHE的配置内容的基础上实现的! 1.安装软件: yum install php -y ##安装完成后,可以在/etc/httpd/conf.d/目录下查看,有php ...
- 配置wampserver 虚拟主机
1.修改http.conf 找到,#Include conf/extra/httpd-vhosts.conf,修改为(有的版本服务器,默认是开启的): 2.配置httpd-vhosts.conf文件, ...
- libcurl 错误码总结
下载出现这种错误(Requested range was not delivered by the server ),说明是重复下载,删掉本地的再下载就不会出现了
- 博客迁移至“零一积流|it-refer.com”
虽然在博客园写了没几篇文章,考虑到个人发展,自己还是建立一个独立的站点:零一积流. 以后直接在自己网站上写东西了,此处只用做文章收藏.
- ranch分析学习(四)
经过的前面的梳理,整个ranch框架的结构,大致有了一个清晰的脉络,即使我说的不是很清楚大家也基本能阅读懂源码.下面我继续分析剩下的的几个文件. 7.ranch_transport.erl 这个文件是 ...