简述

MyBatis是一种ORM框架,即:对象关系映射。是指数据库和Java对象可相互转化,这是基于JDBC的二次开发。与JDBC不同的是,MyBatis在完成一些配置之后,需要做得仅仅是在一个文件中写sql语句。通过反向代理可大大减轻开发过程中代码冗余的编写。MyBatis的主要内容包括输入映射、输出映射、动态标签库、多表操作语句、MyBatis缓存。

前提

image.png

导包

导入需要用到的依赖。pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.simon</groupId>
  7. <artifactId>my-batis-qucik-start</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <version>5.1.47</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.mybatis</groupId>
  17. <artifactId>mybatis</artifactId>
  18. <version>3.5.7</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.12</version>
  24. <scope>test</scope>
  25. </dependency>
  26. </dependencies>
  27. <!--Maven增强,是为了让Mapper.xml和Mapper接口位于src的同一目录下,当然编译之后还是在同一目录下-->
  28. <build>
  29. <resources>
  30. <resource>
  31. <directory>src/main/resources</directory>
  32. <includes>
  33. <include>**</include>
  34. </includes>
  35. </resource>
  36. <resource>
  37. <directory>src/main/java</directory>
  38. <includes>
  39. <include>
  40. **/*.xml
  41. </include>
  42. </includes>
  43. </resource>
  44. </resources>
  45. </build>
  46. </project>

配置

MyBatis.xml,MyBatis接口

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <properties resource="jdbc.properties"/>
  7. <settings>
  8. <setting name="logImpl" value="STDOUT_LOGGING"/>
  9. </settings>
  10. <typeAliases>
  11. <!--<package name="com.simon.bean"/>-->
  12. <typeAlias type="com.simon.bean.User" alias="user"/>
  13. </typeAliases>
  14. <environments default="development">
  15. <environment id="development">
  16. <transactionManager type="JDBC"/>
  17. <dataSource type="POOLED">
  18. <property name="driver" value="${driverClassName}"/>
  19. <property name="url" value="${url}"/>
  20. <property name="username" value="${username}"/>
  21. <property name="password" value="${password}"/>
  22. </dataSource>
  23. </environment>
  24. </environments>
  25. <mappers>
  26. <mapper resource="com/simon/Mapper.xml"/>
  27. </mappers>
  28. </configuration>
  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/29_db1?useSSL=false&characterEncoding=utf8
  3. username=root
  4. password=123456
  1. package com.simon.bean;
  2. public class User {
  3. private Integer id;
  4. private String username;
  5. private String password;
  6. private Integer age;
  7. @Override
  8. public String toString() {
  9. return "User{" +
  10. "id=" + id +
  11. ", username='" + username + '\'' +
  12. ", password='" + password + '\'' +
  13. ", age=" + age +
  14. '}';
  15. }
  16. public Integer getId() {
  17. return id;
  18. }
  19. public void setId(Integer id) {
  20. this.id = id;
  21. }
  22. public String getUsername() {
  23. return username;
  24. }
  25. public void setUsername(String username) {
  26. this.username = username;
  27. }
  28. public String getPassword() {
  29. return password;
  30. }
  31. public void setPassword(String password) {
  32. this.password = password;
  33. }
  34. public Integer getAge() {
  35. return age;
  36. }
  37. public void setAge(Integer age) {
  38. this.age = age;
  39. }
  40. }

简单的增删改查

  1. package com.simon;
  2. import com.simon.bean.User;
  3. import java.util.List;
  4. public interface Mapper {
  5. User selectUser();
  6. void insertUser(User user);
  7. void deleteUserById(Integer id1,Integer id2,Integer id3);
  8. void updateUserById(Integer id);
  9. List<User> selectUserList();
  10. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.simon.Mapper">
  6. <select id="selectUser" resultType="user">
  7. select * from user where id = 1
  8. </select>
  9. <insert id="insertUser" parameterType="user">
  10. insert into user values(#{id},#{username},#{password},#{age})
  11. </insert>
  12. <delete id="deleteUserById">
  13. delete from user where id = #{param1} or id = #{param2} or id = #{param3}
  14. </delete>
  15. <update id="updateUserById">
  16. update user set age = 99 where id = #{id}
  17. </update>
  18. <select id="selectUserList" resultType="user">
  19. select * from user
  20. </select>
  21. </mapper>

主要内容

输入映射

image.png

  • pom.xml
  • mybatis-config.xml
  • jdbc.properties
  • User类添加注解别名:”user” ```java package com.simon;

import com.simon.bean.User; import org.apache.ibatis.annotations.Param;

import java.util.List; import java.util.Map;

public interface Mapper { User selectUserById(Integer id); User selectUserById2(@Param(“uid”) Integer id);

  1. User selectUserByIdAndUsername(@Param("id") Integer id,
  2. @Param("name") String username);
  3. Integer insertUserByObject(@Param("user") User user);
  4. List<User> selectUserByIdOrUsername(@Param("map") Map map);
  5. List<User> selectUserByIndex(Integer id,String username);
  6. List<User> selectUserByTableName(@Param("tableName") String tableName);
  7. List<User> selectUserByColumnName(@Param("columnName") String columnName);

}

  1. ```xml
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <!DOCTYPE mapper
  4. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <mapper namespace="com.simon.Mapper">
  7. <select id="selectUserById" resultType="user">
  8. select * from user where id = #{id}
  9. </select>
  10. <select id="selectUserById2" resultType="user">
  11. select * from user where id = #{uid}
  12. </select>
  13. <select id="selectUserByIdAndUsername" resultType="user">
  14. select * from user where id = #{id} and username = #{name}
  15. </select>
  16. <insert id="insertUserByObject" parameterType="user">
  17. insert into user values(#{user.id},#{user.username},#{user.password},#{user.age})
  18. </insert>
  19. <select id="selectUserByIdOrUsername" resultType="user">
  20. select * from user where id = #{map.id} or username = #{map.username}
  21. </select>
  22. <select id="selectUserByIndex" resultType="user">
  23. select * from user where id = #{param1} or username = #{param2}
  24. </select>
  25. <select id="selectUserByTableName" resultType="user">
  26. select * from ${tableName} limit 10
  27. </select>
  28. <select id="selectUserByColumnName" resultType="user">
  29. select * from user group by ${columnName}
  30. </select>
  31. </mapper>
  1. package com.simon;
  2. import com.simon.bean.User;
  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.AfterClass;
  8. import org.junit.BeforeClass;
  9. import org.junit.Test;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. public class ParamTest {
  15. public static SqlSession sqlSession = null;
  16. static Mapper mapper;
  17. @BeforeClass
  18. public static void init() throws IOException {
  19. InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  20. SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
  21. SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
  22. sqlSession = sqlSessionFactory.openSession();
  23. mapper = sqlSession.getMapper(Mapper.class);
  24. }
  25. @AfterClass
  26. public static void destory(){
  27. sqlSession.commit();
  28. sqlSession.close();
  29. }
  30. //一个不带别名简单类型的入参
  31. @Test
  32. public void testSelectUserById(){
  33. User user = mapper.selectUserById(1);
  34. System.out.println(user);
  35. }
  36. //一个带别名的简单类型的入参
  37. @Test
  38. public void testSelectUserById2(){
  39. User user = mapper.selectUserById2(2);
  40. System.out.println(user);
  41. }
  42. //多个带别名的简单类型的入参
  43. @Test
  44. public void testSelectUserByIdAndUsername(){
  45. User user = mapper.selectUserByIdAndUsername(2,"兰钊");
  46. System.out.println(user);
  47. }
  48. //带别名的Object类型的入参
  49. @Test
  50. public void testinsertUserByObject(){
  51. User user = new User();
  52. user.setId(7);
  53. user.setUsername("张三");
  54. user.setPassword("废话转");
  55. user.setAge(78);
  56. Integer affectedRows = mapper.insertUserByObject(user);
  57. System.out.println("affectedRows:" + affectedRows);
  58. }
  59. //通过Map作为入参(不推荐)
  60. @Test
  61. public void testselectUserByIdOrUsername(){
  62. HashMap<String, Object> map = new HashMap<>();
  63. map.put("id",1);
  64. map.put("username","张三");
  65. List<User> userList = mapper.selectUserByIdOrUsername(map);
  66. }
  67. //通过位置作为入参(不推荐)
  68. @Test
  69. public void testselectUserByIndex(){
  70. List<User> userList = mapper.selectUserByIndex(1, "天明");
  71. System.out.println(userList);
  72. }
  73. //$和#:关于$的优势:传递表名或者列名的时候必须使用$,分库分表的时候用得多
  74. @Test
  75. public void testselectUserByTableName(){
  76. List<User> userList = mapper.selectUserByTableName("user");
  77. System.out.println(userList);
  78. }
  79. @Test
  80. public void testselectUserByColumnName(){
  81. List<User> userList = mapper.selectUserByColumnName("age");
  82. System.out.println(userList);
  83. }
  84. }

输出映射

image.png
输出映射,包含单个字段、单个字段列表的输出映射,对象、对象列表的输出映射、ResuletMap输出映射。

单个字段

字段列表,by list

字段列表,by array

单个对象

单个对象列表
单个对象,解决对应不上的问题:Result

对象列表,解决对应不上的问题:Result

  1. package com.simon;
  2. import com.simon.bean.User;
  3. import com.simon.bean.UserVO;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.List;
  6. import java.util.Map;
  7. public interface Mapper {
  8. Integer selectUserAgeById(Integer id);
  9. List<Integer> selectUserAges();
  10. Integer[] selectUserAges2();
  11. User selectUserById(Integer id);
  12. UserVO selectUserById2(Integer id);
  13. List<User> selectUserList();
  14. UserVO selectUserVOById3(Integer id);
  15. List<UserVO> selectUserVOList();
  16. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.simon.Mapper">
  6. <select id="selectUserAgeById" resultType="int">
  7. select age from user where id = #{id}
  8. </select>
  9. <select id="selectUserAges" resultType="int">
  10. select age from user
  11. </select>
  12. <select id="selectUserAges2" resultType="int">
  13. select age from user
  14. </select>
  15. <select id="selectUserById" resultType="com.simon.bean.User">
  16. select * from user where id = #{id}
  17. </select>
  18. <select id="selectUserById2" resultType="com.simon.bean.UserVO">
  19. SELECT
  20. id AS id,
  21. username AS name,
  22. PASSWORD AS passwd,
  23. age AS age
  24. FROM
  25. USER
  26. WHERE
  27. id = #{id}
  28. </select>
  29. <select id="selectUserList" resultType="user">
  30. select * from user
  31. </select>
  32. <!--ResultMap-->
  33. <resultMap id="selectUserVO" type="com.simon.bean.UserVO">
  34. <id column="id" property="id"/>
  35. <result column="username" property="name"/>
  36. <result column="password" property="passwd"/>
  37. <result column="age" property="age"/>
  38. </resultMap>
  39. <select id="selectUserVOById3" resultMap="selectUserVO">
  40. select * from user where id = #{id}
  41. </select>
  42. <select id="selectUserVOList" resultMap="selectUserVO">
  43. select * from user
  44. </select>
  45. </mapper>

image.png

  1. package com.simon;
  2. import com.simon.bean.User;
  3. import com.simon.bean.UserVO;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.AfterClass;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. public class OutPutMappingTest {
  16. public static SqlSession sqlSession = null;
  17. static Mapper mapper;
  18. @BeforeClass
  19. public static void init() throws IOException {
  20. InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  21. SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
  22. SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
  23. sqlSession = sqlSessionFactory.openSession();
  24. mapper = sqlSession.getMapper(Mapper.class);
  25. }
  26. @AfterClass
  27. public static void destory(){
  28. sqlSession.commit();
  29. sqlSession.close();
  30. }
  31. //输出映射,包含单个字段、单个字段列表的输出映射,
  32. //对象、对象列表的输出映射
  33. //ResuletMap输出映射
  34. //单个字段
  35. @Test
  36. public void testSelectUserAgeById(){
  37. Integer age = mapper.selectUserAgeById(2);
  38. System.out.println("age:" + age);
  39. }
  40. //字段列表,by list
  41. @Test
  42. public void testSelectUserAges(){
  43. List<Integer> ages = mapper.selectUserAges();
  44. System.out.println(ages);
  45. }
  46. //字段列表,by array
  47. @Test
  48. public void testSelectUserAges2(){
  49. Integer[] ages = mapper.selectUserAges2();
  50. for (Integer age : ages) {
  51. System.out.println("age:" + age);
  52. }
  53. }
  54. //单个对象
  55. @Test
  56. public void testSelectUserById(){
  57. User user = mapper.selectUserById(2);
  58. System.out.println(user);
  59. }
  60. //单个对象,解决对应不上的问题:起别名
  61. @Test
  62. public void testSelectUserById2(){
  63. UserVO user = mapper.selectUserById2(3);
  64. System.out.println(user);
  65. }
  66. //单个对象列表
  67. @Test
  68. public void testSelectUserList(){
  69. List<User> userList = mapper.selectUserList();
  70. System.out.println(userList);
  71. }
  72. //单个对象,解决对应不上的问题:Result
  73. @Test
  74. public void testSelectUserById3(){
  75. UserVO userVO = mapper.selectUserVOById3(5);
  76. System.out.println(userVO);
  77. }
  78. //对象列表,解决对应不上的问题:Result
  79. @Test
  80. public void testSelectUserVO(){
  81. List<UserVO> userVOList = mapper.selectUserVOList();
  82. for (UserVO userVO : userVOList) {
  83. System.out.println(userVO);
  84. }
  85. }
  86. }

动态标签库

image.png image.png

User类、Mapper接口、Mapper.xml实际在用。

  1. package com.simon;
  2. import com.simon.bean.User;
  3. import com.simon.bean.UserVO;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.AfterClass;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. public class OutPutMappingTest {
  17. public static SqlSession sqlSession = null;
  18. static Mapper mapper;
  19. @BeforeClass
  20. public static void init() throws IOException {
  21. InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  22. SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
  23. SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
  24. sqlSession = sqlSessionFactory.openSession();
  25. mapper = sqlSession.getMapper(Mapper.class);
  26. }
  27. @AfterClass
  28. public static void destory(){
  29. sqlSession.commit();
  30. sqlSession.close();
  31. }
  32. //输出映射,包含单个字段、单个字段列表的输出映射,
  33. //对象、对象列表的输出映射
  34. //ResuletMap输出映射
  35. //where...if
  36. @Test
  37. public void testSelectUserBySelf(){
  38. User user = new User();
  39. user.setId(2);
  40. user = mapper.selectUserBySelf(user);
  41. System.out.println(user);
  42. }
  43. //if...else...---->choose..when..otherwise..
  44. @Test
  45. public void testSelectUserListByAge(){
  46. List<User> users = mapper.selectUserListByAge(28);
  47. System.out.println(users);
  48. }
  49. //传什么条件,改什么条件。trim set,set更简洁
  50. @Test
  51. public void testUpdateUserWithTrim(){
  52. User user = new User();
  53. user.setPassword("我的快餐名!");
  54. user.setId(7);
  55. Integer affectedRows = mapper.updateUserWithTrim(user);
  56. System.out.println("affectedRows:" + affectedRows);
  57. }
  58. @Test
  59. public void testUpdateUserWithSet(){
  60. User user = new User();
  61. user.setPassword("我的第二个快餐名!");
  62. user.setId(7);
  63. Integer affectedRows = mapper.updateUserWithSet(user);
  64. System.out.println("affectedRows:" + affectedRows);
  65. }
  66. //sql...include 抽取公共sql片段,注意怎样抽取
  67. @Test
  68. public void testSelectUserWithSqlIncludes(){
  69. List<User> userList = mapper.selectUserWithSqlIncludes();
  70. System.out.println(userList);
  71. }
  72. //foreach....循环 :批量插入、通过in查找
  73. //注意属性collection,若未指定注解,则以list或array为默认
  74. @Test
  75. public void testInsertUserForeach(){
  76. User user1 = new User();
  77. user1.setId(8);
  78. user1.setUsername("张阿发");
  79. user1.setPassword("恭喜发财");
  80. user1.setAge(37);
  81. User user2 = new User();
  82. user2.setId(9);
  83. user2.setUsername("李孝九");
  84. user2.setPassword("999,感冒灵");
  85. user2.setAge(89);
  86. ArrayList<User> users = new ArrayList<>();
  87. users.add(user1);
  88. users.add(user2);
  89. Integer affectedRows = mapper.insertUserWithForeach(users);
  90. System.out.println("affectedRows:" + affectedRows);
  91. }
  92. @Test
  93. public void testSelectUserWithForeach(){
  94. ArrayList<Integer> list = new ArrayList<>();
  95. for (int i = 0; i < 6; i++) {
  96. list.add(1+i);
  97. }
  98. List<User> userList = mapper.selectUserByIdListWithForeach(list);
  99. System.out.println(userList);
  100. }
  101. //主键自增,一般要先插入后查询。其优化之道是selectKey
  102. @Test
  103. public void testInsertUserThenSelectIdWithSelectKey(){
  104. User user = new User();
  105. user.setId(null);
  106. user.setUsername("金十顺");
  107. user.setPassword("十全十美");
  108. user.setAge(47);
  109. Integer affectedRows = mapper.insertUserThenSelectIdWithSelectKey(user);
  110. System.out.println("affectedRows:" + affectedRows);
  111. System.out.println("获取到的用户id:" + user.getId());
  112. }
  113. //使用useGeneratedKeys比SelectKey更简单
  114. @Test
  115. public void testInsertUserThenSelectIdWithGeneratedKeys(){
  116. User user = new User();
  117. user.setId(null);
  118. user.setUsername("张良");
  119. user.setPassword("足智多谋");
  120. user.setAge(43);
  121. Integer affectedRows = mapper.insertUserThenSelectIdWithGeneratedKeys(user);
  122. System.out.println("affectedRows:" + affectedRows);
  123. System.out.println("获取到的用户id:" + user.getId());
  124. }
  125. }
  1. package com.simon;
  2. import com.simon.bean.User;
  3. import com.simon.bean.UserVO;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Map;
  8. public interface Mapper {
  9. //动态sql标签库,where--if--choose...when...otherwise
  10. //where,if,20
  11. //where的作用:动态拼接,首个自动取出and/or,不满足则去除where,直接select * from user
  12. User selectUserBySelf(@Param("user") User user);
  13. //if...else...---->choose..when..otherwise..
  14. List<User> selectUserListByAge(@Param("age") Integer age);
  15. //传什么条件,改什么条件。trim set,set更简洁
  16. Integer updateUserWithTrim(@Param("user") User user);
  17. Integer updateUserWithSet(@Param("user") User user);
  18. List<User> selectUserWithSqlIncludes();
  19. Integer insertUserWithForeach(@Param("users") ArrayList<User> users);
  20. List<User> selectUserByIdListWithForeach(@Param("idList") List<Integer> idList);
  21. Integer insertUserThenSelectIdWithSelectKey(@Param("user") User user);
  22. Integer insertUserThenSelectIdWithGeneratedKeys(@Param("user") User user);
  23. }
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.simon.Mapper">
  6. <select id="selectUserBySelf" resultType="com.simon.bean.User">
  7. select * from user
  8. <where>
  9. <if test="user.id != null">
  10. and id = #{user.id}
  11. </if>
  12. <if test="user.username != null">
  13. and username = #{user.username}
  14. </if>
  15. <if test="user.password != null">
  16. and password = #{user.password}
  17. </if>
  18. <if test="user.age != null">
  19. and age = #{user.age}
  20. </if>
  21. </where>
  22. </select>
  23. <select id="selectUserListByAge" resultType="com.simon.bean.User">
  24. select * from user
  25. <where>
  26. <choose >
  27. <when test="age gt 30">
  28. age &gt;= 30
  29. </when>
  30. <otherwise>
  31. age &lt; 30
  32. </otherwise>
  33. </choose>
  34. </where>
  35. </select>
  36. <update id="updateUserWithTrim" parameterType="com.simon.bean.User">
  37. update user
  38. <trim prefix="set" suffixOverrides=",">
  39. <if test="user.username != null">
  40. username = #{user.username},
  41. </if>
  42. <if test="user.password != null">
  43. password = #{user.password},
  44. </if>
  45. <if test="user.age != null">
  46. age = #{user.age},
  47. </if>
  48. </trim>
  49. <where>
  50. id = #{user.id}
  51. </where>
  52. </update>
  53. <update id="updateUserWithSet" parameterType="com.simon.bean.User">
  54. update user
  55. <set>
  56. <if test="user.username != null">
  57. username = #{user.username},
  58. </if>
  59. <if test="user.password != null">
  60. password = #{user.password},
  61. </if>
  62. <if test="user.age != null">
  63. age = #{user.age},
  64. </if>
  65. </set>
  66. <where>
  67. id = #{user.id}
  68. </where>
  69. </update>
  70. <sql id="column_lists">
  71. id,username,password,age
  72. </sql>
  73. <select id="selectUserWithSqlIncludes" resultType="com.simon.bean.User">
  74. select <include refid="column_lists"/> from user
  75. </select>
  76. <insert id="insertUserWithForeach" parameterType="com.simon.bean.User">
  77. insert into user
  78. values <foreach collection="users" item="user" separator=",">
  79. (#{user.id},#{user.username},#{user.password},#{user.age},now)
  80. </foreach>
  81. </insert>
  82. <!--注意此处,是#{id}而不是#{id.id},separator也要写-->
  83. <!--foreach是检验动态sql学得好不好的一个重要方面-->
  84. <select id="selectUserByIdListWithForeach" resultType="com.simon.bean.User">
  85. select <include refid="column_lists"/>
  86. from user
  87. <where>
  88. id in
  89. <foreach collection="idList" separator="," item="id" open="(" close=")">
  90. #{id}
  91. </foreach>
  92. </where>
  93. </select>
  94. <insert id="insertUserThenSelectIdWithSelectKey" parameterType="com.simon.bean.User">
  95. <selectKey order="AFTER" keyColumn="id" keyProperty="user.id" resultType="integer">
  96. select LAST_INSERT_ID()
  97. </selectKey>
  98. insert into user values (#{user.id},#{user.username},#{user.password},#{user.age})
  99. </insert>
  100. <insert id="insertUserThenSelectIdWithGeneratedKeys" parameterType="com.simon.bean.User"
  101. useGeneratedKeys="true" keyProperty="user.id" keyColumn="id">
  102. insert into user values (#{user.id},#{user.username},#{user.password},#{user.age})
  103. </insert>
  104. </mapper>