mybatis的动态sql
1、domain类
1 package com.xiaostudy.mybatis.domain; 2 3 /** 4 * @desc domain类 5 * @author xiaostudy 6 * 7 */ 8 public class User { 9 private int id; 10 private String username; 11 private String password; 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public String getUsername() { 22 return username; 23 } 24 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 29 public String getPassword() { 30 return password; 31 } 32 33 public void setPassword(String password) { 34 this.password = password; 35 } 36 37 @Override 38 public String toString() { 39 return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; 40 } 41 42 }
2、代理的dao接口
1 package com.xiaostudy.mybatis.mapper; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import com.xiaostudy.mybatis.dao.SetGetDomain; 7 import com.xiaostudy.mybatis.domain.User; 8 9 /** 10 * @desc dao接口 11 * @author xiaostudy 12 * 13 */ 14 public interface MapperDao { 15 16 /** 17 * @desc 根据用户ID查询用户信息 18 * @param id 参数 19 * @return User 返回类型 20 * @throws Exception 异常 21 */ 22 public User findUserById(int id) throws Exception; 23 24 /** 25 * @desc 根据用户的综合信息来查询用户 26 * @param user 参数 27 * @return User 返回类型 28 * @throws Exception 异常 29 */ 30 public User findUser(SetGetDomain user) throws Exception; 31 32 /** 33 * @desc 根据用户名称查询用户信息 34 * @param username 参数 35 * @return User 返回类型 36 * @throws Exception 异常 37 */ 38 public User findUserByUsername(String username) throws Exception; 39 40 /** 41 * @desc 根据用户名称模糊查询list集合 42 * @param username 参数 43 * @return List<User> 返回类型 44 * @throws IOException 异常 45 */ 46 public List<User> findUserByUsernames(String username) throws IOException; 47 48 /** 49 * @desc 查询所有用户 50 * @return List<User> 返回类型 51 * @throws IOException 异常 52 */ 53 public List<User> findAllUser() throws IOException; 54 55 /** 56 * @desc 添加用户 57 * @param user 参数 58 * @return int 返回类型 59 * @throws IOException 异常 60 */ 61 public int insertUser(User user) throws IOException; 62 63 /** 64 * @desc 根据用户id修改用户名称 65 * @param user 参数 66 * @return int 返回类型 67 * @throws IOException 异常 68 */ 69 public int updateUserUsername(User user) throws IOException; 70 71 /** 72 * @desc 根据用户id修改用户名称和密码 73 * @param user 参数 74 * @return int 返回类型 75 * @throws IOException 异常 76 */ 77 public int updateUserUsernamePassword(User user) throws IOException; 78 79 }
3、代理的dao配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 5 <!-- namespace:命名空间,对statement的信息进行分类管理 --> 6 <!-- 注意:在mapper代理时,它具有特殊及重要的作用 --> 7 <mapper namespace="com.xiaostudy.mybatis.mapper.MapperDao"> 8 9 <select id="findUser" parameterType="com.xiaostudy.mybatis.dao.SetGetDomain" resultType="user"> 10 SELECT * FROM USER 11 <where> 12 <if test="user != null"> 13 <if test="user.id != null and user.id != ''"> 14 and id=#{id} 15 </if> 16 <if test="user.password != null and user.password !=''"> 17 and password=#{password} 18 </if> 19 </if> 20 </where> 21 </select> 22 23 <!-- 根据用户ID查询用户信息 --> 24 <!-- select:表示一个MappedStatement对象 --> 25 <!-- id:statement的唯一标示 --> 26 <!-- #{}:表示一个占位符? --> 27 <!-- #{id}:里面的id表示输入参数的参数名称,如果该参数是简单类型,那么#{}里面的参数名称可以任意 --> 28 <!-- parameterType:输入参数的java类型 --> 29 <!-- resultType:输出结果的所映射的java类型(单条结果所对应的java类型) --> 30 <select id="findUserById" parameterType="int" resultType="com.xiaostudy.mybatis.domain.User"> 31 SELECT * FROM USER WHERE id = #{id} 32 </select> 33 34 <!-- 根据用户名称查询指定用户 --> 35 <select id="findUserByUsername" parameterType="java.lang.String" resultType="com.xiaostudy.mybatis.domain.User"> 36 SELECT * FROM USER WHERE username = #{username} 37 </select> 38 39 <!-- 根据用户名称模糊查询list集合 --> 40 <select id="findUserByUsernames" parameterType="java.lang.String" resultType="com.xiaostudy.mybatis.domain.User"> 41 SELECT * FROM USER WHERE username LIKE #{username} 42 </select> 43 44 <!-- 根据所有用户 --> 45 <select id="findAllUser" resultType="com.xiaostudy.mybatis.domain.User"> 46 SELECT * FROM USER 47 </select> 48 49 <!-- 添加用户 --> 50 <insert id="insertUser" parameterType="com.xiaostudy.mybatis.domain.User"> 51 <selectKey keyProperty="id" resultType="int" order="AFTER"> 52 select last_insert_id() 53 </selectKey> 54 insert into user (username, password) values (#{username}, #{password}) 55 </insert> 56 57 <!-- 修改用户名称 --> 58 <update id="updateUserUsername" parameterType="com.xiaostudy.mybatis.domain.User"> 59 update user set username=#{username} where id=#{id} 60 </update> 61 62 <!-- 修改用户名称和密码 --> 63 <update id="updateUserUsernamePassword" parameterType="com.xiaostudy.mybatis.domain.User"> 64 update user set username=#{username}, password=#{password} where id=#{id} 65 </update> 66 67 </mapper>
4、动态sql用到的get类
1 package com.xiaostudy.mybatis.dao; 2 3 import com.xiaostudy.mybatis.domain.User; 4 5 /** 6 * @desc 动态sql用到的get类 7 * @author xiaostudy 8 * 9 */ 10 public class SetGetDomain { 11 12 private User user; 13 14 public User getUser() { 15 return user; 16 } 17 18 public void setUser(User user) { 19 this.user = user; 20 } 21 22 public int getId() { 23 return user.getId(); 24 } 25 26 public String getPassword() { 27 return user.getPassword(); 28 } 29 30 public String getUsername() { 31 return user.getUsername(); 32 } 33 34 35 }
5、测试类
1 package com.xiaostudy.mybatis.mapper; 2 3 import java.io.InputStream; 4 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 10 import com.xiaostudy.mybatis.dao.SetGetDomain; 11 import com.xiaostudy.mybatis.domain.User; 12 13 public class MybatisTest { 14 15 public static void main(String[] args) throws Exception { 16 String resource = "config/SqlMapConfig.xml"; 17 InputStream inputStream = Resources.getResourceAsStream(resource); 18 19 // 创建SqlSessionFactory 20 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 21 22 // 创建SqlSession 23 SqlSession sqlSession = sqlSessionFactory.openSession(); 24 25 //获取一个代理dao实现 26 MapperDao mapperDao = sqlSession.getMapper(MapperDao.class); 27 28 //进行动态sql需要的domain类的get方法类,和domain属性的get方法 29 SetGetDomain setGetDomain = new SetGetDomain(); 30 31 //新建一个domain类 32 User user = new User(); 33 user.setId(8); 34 user.setUsername("haha"); 35 user.setPassword("2019"); 36 37 //通过set方法,把domain包装 38 setGetDomain.setUser(user); 39 40 User user2 = mapperDao.findUser(setGetDomain); 41 42 System.out.println(user2); 43 44 sqlSession.close(); 45 } 46 47 }
6、db.properties
1 db.driver=com.mysql.jdbc.Driver 2 db.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8 3 db.username=root 4 db.password=123456
7、SqlMapConfig.xml
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 7 <!-- 加载java的配置文件 --> 8 <properties resource="config/db.properties"/> 9 10 <typeAliases> 11 <!-- 单个别名定义,动态sql中用到 --> 12 <typeAlias type="com.xiaostudy.mybatis.domain.User" alias="user"/> 13 14 <!-- 批量别名定义(推荐) --> 15 <!-- package:指定包名称来为该包下的po类声明别名,默认的别名就是类名(首字母大小写都可) --> 16 <!-- <package name="com.xiaostudy.mybatis.domain" /> --> 17 </typeAliases> 18 19 <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 --> 20 <environments default="development"> 21 <environment id="development"> 22 <!-- 配置JDBC事务控制,由mybatis进行管理 --> 23 <transactionManager type="JDBC"></transactionManager> 24 <!-- 配置数据源,采用mybatis连接池 --> 25 <dataSource type="POOLED"> 26 <property name="driver" value="${db.driver}" /> 27 <property name="url" value="${db.url}" /> 28 <property name="username" value="${db.username}" /> 29 <property name="password" value="${db.password}" /> 30 </dataSource> 31 </environment> 32 </environments> 33 34 <!-- 加载映射文件 --> 35 <mappers> 36 <mapper resource="config/User.xml" /> 37 <mapper resource="com/xiaostudy/mybatis/mapper/MapperDao.xml" /> 38 </mappers> 39 40 </configuration>
8、log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
讲解