MyBatis框架的使用及源码分析(一) 配置与使用
我们先来看一个例子,简单的了解一下mybatis的mapper接口方式的使用。
1 package org.mybatis.spring.sample; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 import org.junit.Test; 8 import org.mybatis.spring.sample.bean.User; 9 import org.mybatis.spring.sample.mapper.UserMapper; 10 11 import java.io.IOException; 12 13 public class MybatisTest { 14 15 /** 16 * 读取mybatis的配置文件,生成SqlSessionFactory 17 * 18 * @return 19 */ 20 private static SqlSessionFactory getSessionFactory() { 21 SqlSessionFactory sessionFactory = null; 22 String resource = "mybatisConfig.xml"; 23 try { 24 sessionFactory = new SqlSessionFactoryBuilder().build(Resources 25 .getResourceAsReader(resource)); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 return sessionFactory; 30 } 31 32 @Test 33 public void findUserById() { 34 SqlSessionFactory sqlSessionFactory = getSessionFactory(); 35 SqlSession sqlSession = sqlSessionFactory.openSession(); 36 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 37 User user = userMapper.selectByPrimaryKey(1l); 38 System.out.println(user.getId() + " / " + user.getName()); 39 } 40 41 }
输出结果
1 / 赵大
数据库表 user
User.java
1 /* 2 * User.java 3 * Copyright(C) 2015-2017 Jstudio.org 4 * All rights reserved. 5 * -------------------------------------- 6 * 2017-09-17 Created. 7 */ 8 package org.mybatis.spring.sample.bean; 9 10 import java.io.Serializable; 11 12 /** 13 * 14 * This class was generated by MyBatis Generator. 15 * This class corresponds to the database table user 16 * 17 * @mbg.generated do_not_delete_during_merge 2017-09-17 18 */ 19 public class User implements Serializable { 20 /** 21 * 主键 22 */ 23 private Long id; 24 25 /** 26 * 用户名 27 */ 28 private String name; 29 30 /** 31 * 密码 32 */ 33 private String password; 34 35 /** 36 * 电子邮件 37 */ 38 private String email; 39 40 /** 41 * 年龄 42 */ 43 private Integer age; 44 45 private static final long serialVersionUID = 1L; 46 47 /** 48 * 获取主键 49 * 50 * @return id - 主键 51 */ 52 public Long getId() { 53 return id; 54 } 55 56 /** 57 * 设置主键 58 * 59 * @param id 主键 60 */ 61 public void setId(Long id) { 62 this.id = id; 63 } 64 65 /** 66 * 获取用户名 67 * 68 * @return name - 用户名 69 */ 70 public String getName() { 71 return name; 72 } 73 74 /** 75 * 设置用户名 76 * 77 * @param name 用户名 78 */ 79 public void setName(String name) { 80 this.name = name == null ? null : name.trim(); 81 } 82 83 /** 84 * 获取密码 85 * 86 * @return password - 密码 87 */ 88 public String getPassword() { 89 return password; 90 } 91 92 /** 93 * 设置密码 94 * 95 * @param password 密码 96 */ 97 public void setPassword(String password) { 98 this.password = password == null ? null : password.trim(); 99 } 100 101 /** 102 * 获取电子邮件 103 * 104 * @return email - 电子邮件 105 */ 106 public String getEmail() { 107 return email; 108 } 109 110 /** 111 * 设置电子邮件 112 * 113 * @param email 电子邮件 114 */ 115 public void setEmail(String email) { 116 this.email = email == null ? null : email.trim(); 117 } 118 119 /** 120 * 获取年龄 121 * 122 * @return age - 年龄 123 */ 124 public Integer getAge() { 125 return age; 126 } 127 128 /** 129 * 设置年龄 130 * 131 * @param age 年龄 132 */ 133 public void setAge(Integer age) { 134 this.age = age; 135 } 136 }
UserMapper.java
1 /* 2 * UserMapper.java 3 * Copyright(C) 2015-2017 Jstudio.org 4 * All rights reserved. 5 * -------------------------------------- 6 * 2017-09-17 Created. 7 */ 8 package org.mybatis.spring.sample.mapper; 9 10 import org.mybatis.spring.sample.bean.User; 11 12 import java.util.List; 13 14 public interface UserMapper { 15 16 int insert(User entity); 17 18 int insertSelective(User entity); 19 20 int deleteByPrimaryKey(Long id); 21 22 int updateByPrimaryKeySelective(User entity); 23 24 int updateByPrimaryKey(User entity); 25 26 User selectByPrimaryKey(Long id); 27 28 }
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="org.mybatis.spring.sample.mapper.UserMapper"> 4 <!-- This is automatically generated by MyBatis Generator on 2017-09-17. --> 5 <resultMap id="BaseResultMap" type="org.mybatis.spring.sample.bean.User"> 6 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 7 <id column="id" jdbcType="BIGINT" property="id" /> 8 <result column="name" jdbcType="VARCHAR" property="name" /> 9 <result column="password" jdbcType="VARCHAR" property="password" /> 10 <result column="email" jdbcType="VARCHAR" property="email" /> 11 <result column="age" jdbcType="INTEGER" property="age" /> 12 </resultMap> 13 <sql id="Base_Column_List"> 14 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 15 id, name, password, email, age 16 </sql> 17 <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> 18 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 19 select 20 <include refid="Base_Column_List" /> 21 from user 22 where id = #{id,jdbcType=BIGINT} 23 </select> 24 <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> 25 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 26 delete from user 27 where id = #{id,jdbcType=BIGINT} 28 </delete> 29 <insert id="insert" parameterType="org.mybatis.spring.sample.bean.User"> 30 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 31 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> 32 SELECT LAST_INSERT_ID() 33 </selectKey> 34 insert into user (name, password, email, 35 age) 36 values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 37 #{age,jdbcType=INTEGER}) 38 </insert> 39 <insert id="insertSelective" parameterType="org.mybatis.spring.sample.bean.User"> 40 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 41 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> 42 SELECT LAST_INSERT_ID() 43 </selectKey> 44 insert into user 45 <trim prefix="(" suffix=")" suffixOverrides=","> 46 <if test="name != null"> 47 name, 48 </if> 49 <if test="password != null"> 50 password, 51 </if> 52 <if test="email != null"> 53 email, 54 </if> 55 <if test="age != null"> 56 age, 57 </if> 58 </trim> 59 <trim prefix="values (" suffix=")" suffixOverrides=","> 60 <if test="name != null"> 61 #{name,jdbcType=VARCHAR}, 62 </if> 63 <if test="password != null"> 64 #{password,jdbcType=VARCHAR}, 65 </if> 66 <if test="email != null"> 67 #{email,jdbcType=VARCHAR}, 68 </if> 69 <if test="age != null"> 70 #{age,jdbcType=INTEGER}, 71 </if> 72 </trim> 73 </insert> 74 <update id="updateByPrimaryKeySelective" parameterType="org.mybatis.spring.sample.bean.User"> 75 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 76 update user 77 <set> 78 <if test="name != null"> 79 name = #{name,jdbcType=VARCHAR}, 80 </if> 81 <if test="password != null"> 82 password = #{password,jdbcType=VARCHAR}, 83 </if> 84 <if test="email != null"> 85 email = #{email,jdbcType=VARCHAR}, 86 </if> 87 <if test="age != null"> 88 age = #{age,jdbcType=INTEGER}, 89 </if> 90 </set> 91 where id = #{id,jdbcType=BIGINT} 92 </update> 93 <update id="updateByPrimaryKey" parameterType="org.mybatis.spring.sample.bean.User"> 94 <!-- WARNING - This element is automatically generated by MyBatis Generator, modify carefully. --> 95 update user 96 set name = #{name,jdbcType=VARCHAR}, 97 password = #{password,jdbcType=VARCHAR}, 98 email = #{email,jdbcType=VARCHAR}, 99 age = #{age,jdbcType=INTEGER} 100 where id = #{id,jdbcType=BIGINT} 101 </update> 102 </mapper>
mybatisConfig.xml
<?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="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlmapping/UserMapper.xml"/> </mappers> </configuration>