- POJO
- private Integer id;
- private String lastName;
- private String email;
- private String gender;
- //getter and setter
接口
- public interface EmployeeMapper {
- // 查询
- public Employee getEmployeeById(Integer id);
- // 多条件查询
- public Employee getEmpLoyeeByIdAndName(@Param("id") Integer id, @Param("lastName") String lastName);
- // 添加
- public void addEmp(Employee employee);
- // 修改
- public void updateEmp(Employee employee);
- // 删除
- public void deleteEmpById(Integer id);
- }
MyBatis 主配置文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="test">
- <transactionManager type=""></transactionManager>
- <dataSource type=""></dataSource>
- </environment>
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
- <property name="username" value="root" />
- <property name="password" value="123456" />
- </dataSource>
- </environment>
- </environments>
- <!-- 将我们写好的 sql 映射文件 (testEmployeeMapper.xml) 注册到全局配置文件 (mybatis-config.xml) 中 -->
- <mappers>
- <mapper resource="testEmployeeMapper.xml" />
- </mappers>
- </configuration>
测试类
- public class MyBatisTest {
- // 获取 SqlSessionFactory 对象
- private SqlSessionFactory getSqlSessionFactory() throws IOException {
- String resource = "mybatis-config.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- return new SqlSessionFactoryBuilder().build(inputStream);
- }
- /**
- * 测试增删改
- * 1. 需要手动提交数据
- * sqlSessionFactory.openSession(); === 手动提交
- * sqlSessionFactory.openSession(true); === 自动提交
- * 2. 可以有返回值, 返回值类型是一下几种:
- * Long, Integer,Boolean,void
- * @throws IOException
- */
- @Test
- public void test03() throws IOException {
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- // 获取到的 SqlSession 对象不会自动提交数据
- SqlSession openSession = sqlSessionFactory.openSession();
- try {
- EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
- // 测试单个参数查询
- // Employee employee = mapper.getEmployeeById(2);
- // System.out.println(employee);
- // 测试多个参数查询
- Employee employee = mapper.getEmpLoyeeByIdAndName(2, "xiaobai");
- System.out.println(employee);
- // 添加一条数据
- // Employee employee = new Employee(null, "xiaobai", "xiaobai@zgz.com", "0");
- // mapper.addEmp(employee);
- // System.out.println(employee.getId());
- // 更新数据
- // Employee employee = new Employee(1, "xiaobai", "xiaobai@zgz.com", "0");
- // mapper.updateEmp(employee);
- // 删除数据
- // mapper.deleteEmpById(1);
- // 手动提交数据
- // openSession.commit();
- } finally {
- openSession.close();
- }
- }
- }
Sql 映射文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zgz.MyBatis.dao.EmployeeMapper">
- <!--
- namespace: 名称空间
- id: 唯一标识
- resultType: 返回值类型
- #{id}: 从传递过来的参数中取出 id 值
- 在 MyBatis 中, 接口可以与配置文件实现动态绑定, 绑定方式:
- 1. 把 namespace 指定为接口的全类名
- 2. 把 id 换成接口中的方法, 可以把 select 标签和接口中的方法进行绑定
- -->
- <!--
- 使用 #{}取值和 ${}取值的区别:
- #{}: 是以预编译的形式, 将参数设置到 sql 语句中, 防止 sql 注入, 一般均使用 #{}
- ${}: 取出的值直接拼装在 sql 语句中, 会有安全问题, 对于不支持预编译 (不支持占位符) 的原生 jdbc, 可以使用 ${}, 比如 ${tableName};
- eg:
- select * from test where id = #{id} and name = ${name};
- select * from test where id = ? and name = xiaoming;
- -->
- <!--
- 参数传递:
- 单个参数: mybatis 不会做特殊处理
- 用 #{参数名}的形式去除参数值, 参数名可以随便起, 但最好要直观
- 多个参数: mybatis 会对其进行特殊处理
- 多个参数会被封装成一个 map
- key:param1...paramN
- value: 传入的参数值
- #{}就是从 map 中取出参数值, 用 #{param1}或 #{0}取出第一个值, 以此类推取出 n 个值
- 命名参数:#{param1}, 可读性不好, 采用注解的形式增加可读性
- public Employee getEmpLoyeeByIdAndName(@Param("id") Integer id, @Param("lastName") String lastName);
- 传递多个参数:
- 1. POJO:
- 如果参数正好是业务逻辑的数据模型, 可以直接传入 pojo;#{属性名}: 取出传入的 pojo 的属性值
- 2. Map:
- 如果多个参数不是业务逻辑的数据模型, 没有对应的 pojo, 为了方便可以传入 map;#{key}: 取出 map 中的值
- Collection 类型: key 为 collection
- List 类型: key 为 list
- 数组类型: key 为 array
- 3. TO:
- 如果多个参数不是业务模型中的数据, 但是经常使用, 可以编写一个 TO(Transfer Object)数据传输对象
- 例如: 做分页的时候可以传入一个
- Page{
- int index;
- int size;
- }
- -->
- <!-- 单个参数查询 -->
- <select id="getEmployeeById" resultType="com.zgz.MyBatis.bean.Employee">
- select id,last_name lastName,email,gender from tbl_employee where id = #{id}
- </select>
- <!-- 多个参数查询 -->
- <select id="getEmpLoyeeByIdAndName" resultType="com.zgz.MyBatis.bean.Employee">
- select id, last_name lastName, email, gender
- from tbl_employee
- where id = #{id} and last_name = #{lastName}
- </select>
- <!--
- MyBatis 获取自增主键:
- 1. useGeneratedKeys="true", 使用主键自增策略
- 2. keyProperty="id", 指定对应主键属性, 也就是 mybatis 获取到主键后将这个值封装给 javaBean 的哪个属性
- Oracle 不支持主键自增: Oracle 使用序列来模拟自增
- 每次插入的数据的主键是从序列中拿到的值, 如何获取到这个值
- -->
- <!-- 增删改时返回的是影响的行数 -->
- <insert id="addEmp" parameterType="com.zgz.MyBatis.bean.Employee"
- useGeneratedKeys="true" keyProperty="id">
- insert into tbl_employee(last_name, email, gender)
- values(#{lastName},#{email},#{gender})
- </insert>
- <update id="updateEmp" parameterType="com.zgz.MyBatis.bean.Employee">
- update tbl_employee
- set last_name=#{lastName},email=#{email},gender=#{gender}
- where id = #{id}
- </update>
- <delete id="deleteEmpById" parameterType="com.zgz.MyBatis.bean.Employee">
- delete from tbl_employee where id = #{id}
- </delete>
- </mapper>
来源: http://www.bubuko.com/infodetail-2689176.html