一、目录结构
二、代码
1、UserDao
1 package cn.bijian.dao; 2 3 import cn.bijian.model.User; 4 import cn.bijian.model.User2; 5 import cn.bijian.pojo.QueryVo; 6 7 import java.util.List; 8 9 public interface UserDao { 10 /* 11 根据QueryVo中的条件查询用户 12 */ 13 List<User> findByVo(QueryVo queryVo); 14 /* 15 查询所有用户(使用别名) 16 */ 17 List<User2> findAll2(); 18 19 /* 20 查询所有用户(使用resultMap) 21 */ 22 List<User2> findAll3(); 23 }
2、User2
1 package cn.bijian.model; 2 3 import java.util.Date; 4 5 public class User2 { 6 private Integer userId; 7 private String userName; 8 private Date userBirthday; 9 private String userSex; 10 private String userAddress; 11 12 public Integer getUserId() { 13 return userId; 14 } 15 16 public void setUserId(Integer userId) { 17 this.userId = userId; 18 } 19 20 public String getUserName() { 21 return userName; 22 } 23 24 public void setUserName(String userName) { 25 this.userName = userName; 26 } 27 28 public Date getUserBirthday() { 29 return userBirthday; 30 } 31 32 public void setUserBirthday(Date userBirthday) { 33 this.userBirthday = userBirthday; 34 } 35 36 public String getUserSex() { 37 return userSex; 38 } 39 40 public void setUserSex(String userSex) { 41 this.userSex = userSex; 42 } 43 44 public String getUserAddress() { 45 return userAddress; 46 } 47 48 public void setUserAddress(String userAddress) { 49 this.userAddress = userAddress; 50 } 51 52 @Override 53 public String toString() { 54 return "User2{" + 55 "userId=" + userId + 56 ", userName='" + userName + '\'' + 57 ", userBirthday=" + userBirthday + 58 ", userSex='" + userSex + '\'' + 59 ", userAddress='" + userAddress + '\'' + 60 '}'; 61 } 62 }
3、QueryVo
1 package cn.bijian.pojo; 2 3 import cn.bijian.model.User; 4 5 import java.io.Serializable; 6 7 public class QueryVo implements Serializable { 8 private User user; 9 10 public User getUser() { 11 return user; 12 } 13 14 public void setUser(User user) { 15 this.user = user; 16 } 17 18 @Override 19 public String toString() { 20 return "QueryVo{" + 21 "user=" + user + 22 '}'; 23 } 24 }
4、UserDao.xml
<?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="cn.bijian.dao.UserDao"> <select id="findByVo" parameterType="cn.bijian.pojo.QueryVo" resultType="cn.bijian.model.User"> select * from user where username like #{user.username} </select> <!--使用别名--> <select id="findAll2" resultType="cn.bijian.model.User2"> select id as userId,username as userName,birthday as userBirthday,sex as userSex,address as userAddress from user </select> <!-- 使用resultMap--> <resultMap id="userMap" type="cn.bijian.model.User2"> <id column="id" property="userId"></id> <result column="username" property="userName"></result> <result column="birthday" property="userBirthday"></result> <result column="sex" property="userSex"></result> <result column="address" property="userAddress"></result> </resultMap> <select id="findAll3" resultMap="userMap"> select * from user </select> </mapper>
5、db.properties
1 jdbc.driver=com.mysql.cj.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC 3 jdbc.username=root 4 jdbc.password=123456
6、SqlMapConfig.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="mysql"> <environment id="mysql"> <!--JDBC 事务管理器被用作当应用程序负责管理数据库连接的生命周期(提交、回退等等)的时候。当你将 TransactionManager 属性设置成 JDBC,MyBatis 内部将使用 JdbcTransactionFactory 类创建 TransactionManager。例如,部署到 Apache Tomcat 的应用程序,需要应用程序自己管理事务。--> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <!--连接池--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="cn/bijian/dao/UserDao.xml"></mapper>--> <!--用注解方式,需移除dao.xml映射配置--> <!-- <mapper class="cn.bijian.dao.UserDao"></mapper> --> <!--注册指定包下的所有dao接口--> <package name="cn.bijian.dao"/> </mappers> </configuration>
7、MybatisCURDTest2
1 package cn.bijian.test; 2 3 import cn.bijian.dao.UserDao; 4 import cn.bijian.model.User; 5 import cn.bijian.model.User2; 6 import cn.bijian.pojo.QueryVo; 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.junit.After; 12 import org.junit.Before; 13 import org.junit.Test; 14 15 import java.io.IOException; 16 import java.io.InputStream; 17 import java.util.List; 18 19 20 public class MybatisCURDTest2 { 21 private InputStream in; 22 private SqlSessionFactory sessionFactory; 23 private SqlSession session; 24 private UserDao userDao; 25 26 @Test 27 public void testFindByVo(){ 28 User user = new User(); 29 QueryVo queryVo = new QueryVo(); 30 user.setUsername("%王%"); 31 queryVo.setUser(user); 32 List<User> users = userDao.findByVo(queryVo); 33 for (User u:users) { 34 System.out.println(u); 35 } 36 } 37 @Test 38 public void testFindAll2(){ 39 List<User2> users = userDao.findAll2(); 40 for (User2 user:users) { 41 System.out.println(user); 42 } 43 } 44 45 @Test 46 public void testFindAll3(){ 47 List<User2> users = userDao.findAll3(); 48 for (User2 user:users) { 49 System.out.println(user); 50 } 51 } 52 53 @Before 54 public void init() throws IOException { 55 in = Resources.getResourceAsStream("SqlMapConfig.xml"); 56 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 57 sessionFactory = builder.build(in); 58 session = sessionFactory.openSession(); 59 userDao = session.getMapper(UserDao.class); 60 } 61 62 @After 63 public void destroy() throws IOException { 64 session.commit(); 65 session.close(); 66 in.close(); 67 } 68 69 }