MyBatis学习

 初次接触

MyBatis是一种轻量级的ORM框架,相对于Hibernate来说比较简单易用,对于较小的系统或者对于SQL语句有独特爱好的朋友可以使用MyBatis进行开发。网上大多是直接照抄MyBatis官网提供的docs例子进行粘贴,具体能不能用还不得而知。下面以一个例子来介绍MyBatis的基本使用。

一、建立数据库表

创建用户表:

[sql]  view plain copy
  1. DROP TABLE IF EXISTS user;  
  2. CREATE TABLE user (  
  3.   username varchar(50) NOT NULL,  
  4.   password varchar(50) DEFAULT NULL,  
  5.   address varchar(50) DEFAULT NULL,  
  6.   age varchar(5) DEFAULT NULL,  
  7.   sex varchar(2) DEFAULT NULL,  
  8.   PRIMARY KEY (username)  
  9. )  

二、建立对应的DTO对象

[java]  view plain copy
  1. public class UserDto implements java.io.Serializable {  
  2.     private static final long serialVersionUID = 1L;  
  3.     private String username;  
  4.     private String password;  
  5.     private String address;  
  6.     private String age;  
  7.     private String sex;  
  8.   
  9.     public UserDto() {  
  10.     }  
  11.   
  12.     public UserDto(String username, String password, String address, String age, String sex) {  
  13.         this.username = username;  
  14.         this.password = password;  
  15.         this.address = address;  
  16.         this.age = age;  
  17.         this.sex = sex;  
  18.     }  
  19.   
  20.     public String getUsername() {  
  21.         return this.username;  
  22.     }  
  23.   
  24.     public void setUsername(String username) {  
  25.         this.username = username;  
  26.     }  
  27.   
  28.     public String getPassword() {  
  29.         return this.password;  
  30.     }  
  31.   
  32.     public void setPassword(String password) {  
  33.         this.password = password;  
  34.     }  
  35.   
  36.     public String getAddress() {  
  37.         return this.address;  
  38.     }  
  39.   
  40.     public void setAddress(String address) {  
  41.         this.address = address;  
  42.     }  
  43.   
  44.     public String getAge() {  
  45.         return this.age;  
  46.     }  
  47.   
  48.     public void setAge(String age) {  
  49.         this.age = age;  
  50.     }  
  51.   
  52.     public String getSex() {  
  53.         return this.sex;  
  54.     }  
  55.   
  56.     public void setSex(String sex) {  
  57.         this.sex = sex;  
  58.     }  
  59. }  

三、建立MyBatis系统配置文件[mybatis-config.xml]

[html]  view plain copy
  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.     <environments default="development">  
  7.         <environment id="development">  
  8.             <transactionManager type="JDBC" />  
  9.             <dataSource type="POOLED">  
  10.                 <property name="driver" value="com.mysql.jdbc.Driver" />  
  11.                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />  
  12.                 <property name="username" value="test" />  
  13.                 <property name="password" value="test" />  
  14.             </dataSource>  
  15.         </environment>  
  16.     </environments>  
  17.     <mappers>  
  18.         <mapper resource="UserMapper.xml" />  
  19.     </mappers>  
  20. </configuration>  

四、建立user表的映射文件(UserMapper.xml)

[html]  view plain copy
  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.mapper.UserMapper">  
  6.   <select id="selectUser" parameterType="String" resultType="com.dto.UserDto">  
  7.     select * from user where username = #{username}  
  8.   </select>  
  9. </mapper>  

五、建立映射接口类(如果sql语句写在映射的配置XML文件,这一步可以不用)

[java]  view plain copy
  1. package com.mapper;  
  2.   
  3. import org.apache.ibatis.annotations.Select;  
  4.   
  5. import com.dto.UserDto;  
  6.   
  7. public interface UserMapper {  
  8.     @Select("select * from user where username = #{username}")  
  9.     public UserDto seleteUser(String username);  
  10.   
  11. }  

六、建立测试类

[java]  view plain copy
  1. package com.test;  
  2.   
  3.   
  4. import java.io.InputStream;  
  5.   
  6. import org.apache.ibatis.binding.MapperRegistry;  
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.Configuration;  
  9. import org.apache.ibatis.session.SqlSession;  
  10. import org.apache.ibatis.session.SqlSessionFactory;  
  11. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  12. import org.junit.Before;  
  13. import org.junit.Test;  
  14.   
  15. import com.dto.UserDto;  
  16. import com.mapper.UserMapper;  
  17.   
  18. public class UserTest {  
  19.     SqlSessionFactory sqlSessionFactory = null;  
  20.     @Before  
  21.     public void setUp() throws Exception {  
  22.         String resource = "mybatis-config.xml";  
  23.         InputStream inputStream = Resources.getResourceAsStream(resource);  
  24.         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
  25.         Configuration configuration = sqlSessionFactory.getConfiguration();  
  26.         MapperRegistry mapperRegistry = configuration.getMapperRegistry();  
  27.         System.out.println(mapperRegistry.getMappers());  
  28.           
  29.     }  
  30.     @Test  
  31.     public void seleteXMLTest(){  
  32.         SqlSession session = sqlSessionFactory.openSession();  
  33.         try {  
  34.           UserDto userDto = (UserDto) session.selectOne("com.mapper.UserMapper.selectUser""xyz");  
  35.           System.out.println(userDto.getUsername());  
  36.           System.out.println(userDto.getPassword());  
  37.           System.out.println(userDto.getAddress());  
  38.           System.out.println(userDto.getAge());  
  39.           System.out.println(userDto.getSex());  
  40.         } finally {  
  41.           session.close();  
  42.         }  
  43.     }  
  44.     @Test  
  45.     public void seleteAnnotaionTest(){  
  46.         SqlSession session = sqlSessionFactory.openSession();  
  47.         try {  
  48.             UserMapper userMapper = session.getMapper(UserMapper.class);  
  49.             UserDto userDto = userMapper.seleteUser("xyz");  
  50.             System.out.println(userDto.getUsername());  
  51.             System.out.println(userDto.getPassword());  
  52.             System.out.println(userDto.getAddress());  
  53.             System.out.println(userDto.getAge());  
  54.             System.out.println(userDto.getSex());  
  55.         } finally {  
  56.             session.close();  
  57.         }  
  58.     }  
  59.   
  60. }  

 MyBatis3.2.2的源码下载地址:http://download.csdn.net/detail/wyc_cs/5272910



完成简单的增、删、改、查操作

使用MyBatis进行数据库增删改查的操作是很简单的,主要在配置文件中写好相应的SQL语句,然后在程序中进行调用即可。具体代码如下:

一、映射配置文件

[html]  view plain copy
  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.mapper.UserMapper">  
  6.   <select id="selectUser" parameterType="String" resultType="com.dto.UserDto">  
  7.     select * from user where username = #{username}  
  8.   </select>  
  9.   <select id="selectAllUser" resultType="com.dto.UserDto">  
  10.     select * from user   
  11.   </select>  
  12.   <insert id="insertUser" parameterType="com.dto.UserDto">  
  13.     insert into user(username,password,address,age,sex)  
  14.     values (#{username},#{password},#{address},#{age},#{sex})  
  15.   </insert>  
  16.   <update id="updateUser" parameterType="com.dto.UserDto">  
  17.     update user set password=#{password},address=#{address},age=#{age},sex=#{sex}  
  18.     where username=#{username}  
  19.   </update>  
  20.   <delete id="deleteUser" parameterType="String">  
  21.     delete from user where username=#{username}  
  22.   </delete>  
  23.   <delete id="deleteAllUser">  
  24.     delete from user  
  25.   </delete>  
  26. </mapper>  

二、测试类

[java]  view plain copy
  1. package com.test;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.List;  
  5.   
  6. import org.apache.ibatis.binding.MapperRegistry;  
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.Configuration;  
  9. import org.apache.ibatis.session.SqlSession;  
  10. import org.apache.ibatis.session.SqlSessionFactory;  
  11. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  12. import org.junit.Before;  
  13. import org.junit.Test;  
  14.   
  15. import com.dto.UserDto;  
  16. import com.mapper.UserMapper;  
  17.   
  18. public class UserTest{  
  19.     SqlSessionFactory sqlSessionFactory = null;  
  20.   
  21.     @Before  
  22.     public void setUp() throws Exception {  
  23.         String resource = "mybatis-config.xml";  
  24.         InputStream inputStream = Resources.getResourceAsStream(resource);  
  25.         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
  26.     }  
  27.   
  28.     @Test  
  29.     public void seleteXMLTest() {  
  30.         SqlSession session = sqlSessionFactory.openSession();  
  31.         try {  
  32.             UserDto userDto = (UserDto) session.selectOne("com.mapper.UserMapper.selectUser""test");  
  33.             System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());  
  34.         } finally {  
  35.             session.close();  
  36.         }  
  37.     }  
  38.     @Test  
  39.     public void seleteAllXMLTest() {  
  40.         SqlSession session = sqlSessionFactory.openSession();  
  41.         try {  
  42.             List<UserDto> list = session.selectList("com.mapper.UserMapper.selectAllUser");  
  43.             for(UserDto userDto : list){  
  44.                 System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());  
  45.             }  
  46.         } finally {  
  47.             session.close();  
  48.         }  
  49.     }  
  50.   
  51.     @Test  
  52.     public void seleteAnnotaionTest() {  
  53.         SqlSession session = sqlSessionFactory.openSession();  
  54.         try {  
  55.             UserMapper userMapper = session.getMapper(UserMapper.class);  
  56.             UserDto userDto = userMapper.seleteUser("test");  
  57.             System.out.println(userDto.getUsername()+"\t"+userDto.getPassword()+"\t"+userDto.getAddress()+"\t"+userDto.getAge()+"\t"+userDto.getSex());  
  58.         } finally {  
  59.             session.close();  
  60.         }  
  61.     }  
  62.   
  63.     @Test  
  64.     public void insertXMLTest() {  
  65.         SqlSession session = sqlSessionFactory.openSession();  
  66.         try {  
  67.             UserDto userDto = new UserDto();  
  68.             userDto.setUsername("test");  
  69.             userDto.setPassword("test");  
  70.             userDto.setAddress("测试地址......");  
  71.             userDto.setAge("30");  
  72.             userDto.setSex("男");  
  73.             int res = session.insert("com.mapper.UserMapper.insertUser", userDto);  
  74.             session.commit();  
  75.             System.out.println("res = " + res);  
  76.         } finally {  
  77.             session.close();  
  78.         }  
  79.     }  
  80.     @Test  
  81.     public void updateXMLTest() {  
  82.         SqlSession session = sqlSessionFactory.openSession();  
  83.         try {  
  84.             UserDto userDto = new UserDto();  
  85.             userDto.setUsername("test");  
  86.             userDto.setPassword("111111");  
  87.             userDto.setAddress("地址");  
  88.             userDto.setAge("33");  
  89.             userDto.setSex("女");  
  90.             int res = session.update("com.mapper.UserMapper.updateUser", userDto);  
  91.             session.commit();  
  92.             System.out.println("res = " + res);  
  93.         } finally {  
  94.             session.close();  
  95.         }  
  96.     }  
  97.     @Test  
  98.     public void deleteXMLTest() {  
  99.         SqlSession session = sqlSessionFactory.openSession();  
  100.         try {  
  101.             int res = session.delete("com.mapper.UserMapper.deleteUser""test");  
  102.             session.commit();  
  103.             System.out.println("res = " + res);  
  104.         } finally {  
  105.             session.close();  
  106.         }  
  107.     }  
  108.     @Test  
  109.     public void deleteAllXMLTest() {  
  110.         SqlSession session = sqlSessionFactory.openSession();  
  111.         try {  
  112.             int res = session.delete("com.mapper.UserMapper.deleteAllUser");  
  113.             session.commit();  
  114.             System.out.println("res = " + res);  
  115.         } finally {  
  116.             session.close();  
  117.         }  
  118.     }  
  119. }  

注意:在更新操作完成之后需要调用session.commit()方法,不然数据不会更新到数据库中。


使用SqlBuilder生成SQL语句


在MyBatis的映射配置文件中写sql语句有时候很方便,但是对于有大量字段的表结构却不太简单,幸好MyBatis提供的有SqlBuilder工具类,可以生成相应的SQL语句,如下例程:

[java]  view plain copy
  1. package com.utils;  
  2.   
  3. import org.apache.ibatis.jdbc.SqlBuilder;  
  4.   
  5. public class MyBatisUtils extends SqlBuilder {  
  6.     public String selectUserSql() {  
  7.         BEGIN();  
  8.         SELECT("*");  
  9.         FROM("UserDto");  
  10.         return SQL();  
  11.     }  
  12.   
  13.     public String deleteUserSql() {  
  14.         BEGIN();  
  15.         DELETE_FROM("UserDto");  
  16.         WHERE("username = #{username}");  
  17.         return SQL();  
  18.     }  
  19.   
  20.     public String insertUserSql() {  
  21.         BEGIN();  
  22.         INSERT_INTO("UserDto");  
  23.         VALUES("username""#{username}");  
  24.         VALUES("password""#{password}");  
  25.         VALUES("address""#{address}");  
  26.         VALUES("age""#{age}");  
  27.         VALUES("sex""#{sex}");  
  28.         return SQL();  
  29.     }  
  30.   
  31.     public String updateUserSql() {  
  32.         BEGIN();  
  33.         UPDATE("UserDto");  
  34.         SET("password = #{password}");  
  35.         WHERE("username = #{username}");  
  36.         return SQL();  
  37.     }  
  38.   
  39.     public static void main(String[] args) {  
  40.         MyBatisUtils myBatisUtils = new MyBatisUtils();  
  41.         System.out.println("查询 = " + myBatisUtils.selectUserSql());  
  42.         System.out.println("删除 = " + myBatisUtils.deleteUserSql());  
  43.         System.out.println("插入 = " + myBatisUtils.insertUserSql());  
  44.         System.out.println("更新 = " + myBatisUtils.updateUserSql());  
  45.     }  
  46. }  

使用MyBatis_Generator生成Dto、Dao、Mapping

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。

一、建立表结构

CREATE TABLE `user` (
  `id` varchar(50) NOT NULL,
  `username` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(18) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `name` varchar(18) DEFAULT NULL,
  `sex` varchar(2) DEFAULT NULL,
  `birthday` varchar(50) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `tel` varchar(18) DEFAULT NULL,
  `qq` varchar(18) DEFAULT NULL,
  `image` varchar(50) DEFAULT NULL,
  `sfjh` varchar(1) DEFAULT NULL,
  `sfzx` varchar(1) DEFAULT NULL,
  `sfhf` varchar(1) DEFAULT NULL,
  `sfpl` varchar(1) DEFAULT NULL,
  `sffx` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;

二、下载mybatis-generator-core

进入:http://code.google.com/p/mybatis/

选择Downloads,再选择MyBatis Generator Tool下载即可。

三、生成配置文件

新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名。最好将这个文件放在下载后的lib目录中,如图:


其中mysql的驱动可以随便放在非中文路径的地方,这里为了方便就放在lib目录下。

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.xml这个文件的具体内容:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5. <generatorConfiguration>  
  6. <!-- 数据库驱动-->  
  7.     <classPathEntry  location="mysql-connector-java-5.0.6-bin.jar"/>  
  8.     <context id="DB2Tables"  targetRuntime="MyBatis3">  
  9.         <commentGenerator>  
  10.             <property name="suppressDate" value="true"/>  
  11.             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
  12.             <property name="suppressAllComments" value="true"/>  
  13.         </commentGenerator>  
  14.         <!--数据库链接URL,用户名、密码 -->  
  15.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="test" password="test">  
  16.         </jdbcConnection>  
  17.         <javaTypeResolver>  
  18.             <property name="forceBigDecimals" value="false"/>  
  19.         </javaTypeResolver>  
  20.         <!-- 生成模型的包名和位置-->  
  21.         <javaModelGenerator targetPackage="test.model" targetProject="src">  
  22.             <property name="enableSubPackages" value="true"/>  
  23.             <property name="trimStrings" value="true"/>  
  24.         </javaModelGenerator>  
  25.         <!-- 生成映射文件的包名和位置-->  
  26.         <sqlMapGenerator targetPackage="test.mapping" targetProject="src">  
  27.             <property name="enableSubPackages" value="true"/>  
  28.         </sqlMapGenerator>  
  29.         <!-- 生成DAO的包名和位置-->  
  30.         <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">  
  31.             <property name="enableSubPackages" value="true"/>  
  32.         </javaClientGenerator>  
  33.         <!-- 要生成哪些表-->  
  34.         <table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  35.         <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  36.         <table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  37.     </context>  
  38. </generatorConfiguration>  

1、其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。


四、运行

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:


生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping,如图:


下面可以看看生成后的UserMapper.xml

[html]  view plain copy
  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="test.dao.UserDtoMapper" >  
  4.   <resultMap id="BaseResultMap" type="test.model.UserDto" >  
  5.     <id column="id" property="id" jdbcType="VARCHAR" />  
  6.     <result column="username" property="username" jdbcType="VARCHAR" />  
  7.     <result column="password" property="password" jdbcType="VARCHAR" />  
  8.     <result column="email" property="email" jdbcType="VARCHAR" />  
  9.     <result column="name" property="name" jdbcType="VARCHAR" />  
  10.     <result column="sex" property="sex" jdbcType="VARCHAR" />  
  11.     <result column="birthday" property="birthday" jdbcType="VARCHAR" />  
  12.     <result column="address" property="address" jdbcType="VARCHAR" />  
  13.     <result column="tel" property="tel" jdbcType="VARCHAR" />  
  14.     <result column="qq" property="qq" jdbcType="VARCHAR" />  
  15.     <result column="image" property="image" jdbcType="VARCHAR" />  
  16.     <result column="sfjh" property="sfjh" jdbcType="VARCHAR" />  
  17.     <result column="sfzx" property="sfzx" jdbcType="VARCHAR" />  
  18.     <result column="sfhf" property="sfhf" jdbcType="VARCHAR" />  
  19.     <result column="sfpl" property="sfpl" jdbcType="VARCHAR" />  
  20.     <result column="sffx" property="sffx" jdbcType="VARCHAR" />  
  21.   </resultMap>  
  22.   <sql id="Base_Column_List" >  
  23.     id, username, password, email, name, sex, birthday, address, tel, qq, image, sfjh,   
  24.     sfzx, sfhf, sfpl, sffx  
  25.   </sql>  
  26.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >  
  27.     select   
  28.     <include refid="Base_Column_List" />  
  29.     from user  
  30.     where id = #{id,jdbcType=VARCHAR}  
  31.   </select>  
  32.   <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >  
  33.     delete from user  
  34.     where id = #{id,jdbcType=VARCHAR}  
  35.   </delete>  
  36.   <insert id="insert" parameterType="test.model.UserDto" >  
  37.     insert into user (id, username, password,   
  38.       email, name, sex, birthday,   
  39.       address, tel, qq, image,   
  40.       sfjh, sfzx, sfhf, sfpl,   
  41.       sffx)  
  42.     values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},   
  43.       #{email,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR},   
  44.       #{address,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR},   
  45.       #{sfjh,jdbcType=VARCHAR}, #{sfzx,jdbcType=VARCHAR}, #{sfhf,jdbcType=VARCHAR}, #{sfpl,jdbcType=VARCHAR},   
  46.       #{sffx,jdbcType=VARCHAR})  
  47.   </insert>  
  48.   <insert id="insertSelective" parameterType="test.model.UserDto" >  
  49.     insert into user  
  50.     <trim prefix="(" suffix=")" suffixOverrides="," >  
  51.       <if test="id != null" >  
  52.         id,  
  53.       </if>  
  54.       <if test="username != null" >  
  55.         username,  
  56.       </if>  
  57.       <if test="password != null" >  
  58.         password,  
  59.       </if>  
  60.       <if test="email != null" >  
  61.         email,  
  62.       </if>  
  63.       <if test="name != null" >  
  64.         name,  
  65.       </if>  
  66.       <if test="sex != null" >  
  67.         sex,  
  68.       </if>  
  69.       <if test="birthday != null" >  
  70.         birthday,  
  71.       </if>  
  72.       <if test="address != null" >  
  73.         address,  
  74.       </if>  
  75.       <if test="tel != null" >  
  76.         tel,  
  77.       </if>  
  78.       <if test="qq != null" >  
  79.         qq,  
  80.       </if>  
  81.       <if test="image != null" >  
  82.         image,  
  83.       </if>  
  84.       <if test="sfjh != null" >  
  85.         sfjh,  
  86.       </if>  
  87.       <if test="sfzx != null" >  
  88.         sfzx,  
  89.       </if>  
  90.       <if test="sfhf != null" >  
  91.         sfhf,  
  92.       </if>  
  93.       <if test="sfpl != null" >  
  94.         sfpl,  
  95.       </if>  
  96.       <if test="sffx != null" >  
  97.         sffx,  
  98.       </if>  
  99.     </trim>  
  100.     <trim prefix="values (" suffix=")" suffixOverrides="," >  
  101.       <if test="id != null" >  
  102.         #{id,jdbcType=VARCHAR},  
  103.       </if>  
  104.       <if test="username != null" >  
  105.         #{username,jdbcType=VARCHAR},  
  106.       </if>  
  107.       <if test="password != null" >  
  108.         #{password,jdbcType=VARCHAR},  
  109.       </if>  
  110.       <if test="email != null" >  
  111.         #{email,jdbcType=VARCHAR},  
  112.       </if>  
  113.       <if test="name != null" >  
  114.         #{name,jdbcType=VARCHAR},  
  115.       </if>  
  116.       <if test="sex != null" >  
  117.         #{sex,jdbcType=VARCHAR},  
  118.       </if>  
  119.       <if test="birthday != null" >  
  120.         #{birthday,jdbcType=VARCHAR},  
  121.       </if>  
  122.       <if test="address != null" >  
  123.         #{address,jdbcType=VARCHAR},  
  124.       </if>  
  125.       <if test="tel != null" >  
  126.         #{tel,jdbcType=VARCHAR},  
  127.       </if>  
  128.       <if test="qq != null" >  
  129.         #{qq,jdbcType=VARCHAR},  
  130.       </if>  
  131.       <if test="image != null" >  
  132.         #{image,jdbcType=VARCHAR},  
  133.       </if>  
  134.       <if test="sfjh != null" >  
  135.         #{sfjh,jdbcType=VARCHAR},  
  136.       </if>  
  137.       <if test="sfzx != null" >  
  138.         #{sfzx,jdbcType=VARCHAR},  
  139.       </if>  
  140.       <if test="sfhf != null" >  
  141.         #{sfhf,jdbcType=VARCHAR},  
  142.       </if>  
  143.       <if test="sfpl != null" >  
  144.         #{sfpl,jdbcType=VARCHAR},  
  145.       </if>  
  146.       <if test="sffx != null" >  
  147.         #{sffx,jdbcType=VARCHAR},  
  148.       </if>  
  149.     </trim>  
  150.   </insert>  
  151.   <update id="updateByPrimaryKeySelective" parameterType="test.model.UserDto" >  
  152.     update user  
  153.     <set >  
  154.       <if test="username != null" >  
  155.         username = #{username,jdbcType=VARCHAR},  
  156.       </if>  
  157.       <if test="password != null" >  
  158.         password = #{password,jdbcType=VARCHAR},  
  159.       </if>  
  160.       <if test="email != null" >  
  161.         email = #{email,jdbcType=VARCHAR},  
  162.       </if>  
  163.       <if test="name != null" >  
  164.         name = #{name,jdbcType=VARCHAR},  
  165.       </if>  
  166.       <if test="sex != null" >  
  167.         sex = #{sex,jdbcType=VARCHAR},  
  168.       </if>  
  169.       <if test="birthday != null" >  
  170.         birthday = #{birthday,jdbcType=VARCHAR},  
  171.       </if>  
  172.       <if test="address != null" >  
  173.         address = #{address,jdbcType=VARCHAR},  
  174.       </if>  
  175.       <if test="tel != null" >  
  176.         tel = #{tel,jdbcType=VARCHAR},  
  177.       </if>  
  178.       <if test="qq != null" >  
  179.         qq = #{qq,jdbcType=VARCHAR},  
  180.       </if>  
  181.       <if test="image != null" >  
  182.         image = #{image,jdbcType=VARCHAR},  
  183.       </if>  
  184.       <if test="sfjh != null" >  
  185.         sfjh = #{sfjh,jdbcType=VARCHAR},  
  186.       </if>  
  187.       <if test="sfzx != null" >  
  188.         sfzx = #{sfzx,jdbcType=VARCHAR},  
  189.       </if>  
  190.       <if test="sfhf != null" >  
  191.         sfhf = #{sfhf,jdbcType=VARCHAR},  
  192.       </if>  
  193.       <if test="sfpl != null" >  
  194.         sfpl = #{sfpl,jdbcType=VARCHAR},  
  195.       </if>  
  196.       <if test="sffx != null" >  
  197.         sffx = #{sffx,jdbcType=VARCHAR},  
  198.       </if>  
  199.     </set>  
  200.     where id = #{id,jdbcType=VARCHAR}  
  201.   </update>  
  202.   <update id="updateByPrimaryKey" parameterType="test.model.UserDto" >  
  203.     update user  
  204.     set username = #{username,jdbcType=VARCHAR},  
  205.       password = #{password,jdbcType=VARCHAR},  
  206.       email = #{email,jdbcType=VARCHAR},  
  207.       name = #{name,jdbcType=VARCHAR},  
  208.       sex = #{sex,jdbcType=VARCHAR},  
  209.       birthday = #{birthday,jdbcType=VARCHAR},  
  210.       address = #{address,jdbcType=VARCHAR},  
  211.       tel = #{tel,jdbcType=VARCHAR},  
  212.       qq = #{qq,jdbcType=VARCHAR},  
  213.       image = #{image,jdbcType=VARCHAR},  
  214.       sfjh = #{sfjh,jdbcType=VARCHAR},  
  215.       sfzx = #{sfzx,jdbcType=VARCHAR},  
  216.       sfhf = #{sfhf,jdbcType=VARCHAR},  
  217.       sfpl = #{sfpl,jdbcType=VARCHAR},  
  218.       sffx = #{sffx,jdbcType=VARCHAR}  
  219.     where id = #{id,jdbcType=VARCHAR}  
  220.   </update>  
  221. </mapper>  

接下来就可以将这三个目录拷贝到对应项目的目录中,如果需要新增自己的方法可以修改dao类。

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值