package com.itheima.dao; import com.itheima.domain.QueryVo; import com.itheima.domain.User; import java.util.List; /** * @author 黑马程序员 * @Company * * 用户的持久层接口 */ public interface IUserDao { /** * 查询所有用户 * @return */ List<User> findAll(); /** * 根据id查询用户信息 * @param userId * @return */ User findById(Integer userId); /** * 根据名称模糊查询用户信息 * @param username * @return */ List<User> findByName(String username); /** * 根据queryVo中的条件查询用户 * @param vo * @return */ List<User> findUserByVo(QueryVo vo); /** * 根据传入参数条件 * @param user 查询的条件:有可能有用户名,有可能有性别,也有可能有地址,还有可能是都有 * @return */ List<User> findUserByCondition(User user); /** * 根据queryvo中提供的id集合,查询用户信息 * @param vo * @return */ List<User> findUserInIds(QueryVo vo); }
<?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="com.itheima.dao.IUserDao"> <!-- 配置 查询结果的列名和实体类的属性名的对应关系 --> <resultMap id="userMap" type="uSeR"> <!-- 主键字段的对应 --> <id property="userId" column="id"></id> <!--非主键字段的对应--> <result property="userName" column="username"></result> <result property="userAddress" column="address"></result> <result property="userSex" column="sex"></result> <result property="userBirthday" column="birthday"></result> </resultMap> <!-- 了解的内容:抽取重复的sql语句--> <sql id="defaultUser"> select * from user </sql> <!-- 查询所有 --> <select id="findAll" resultMap="userMap"> <include refid="defaultUser"></include> </select> <!-- 根据id查询用户 --> <select id="findById" parameterType="INT" resultMap="userMap"> select * from user where id = #{uid} </select> <!-- 根据名称模糊查询 --> <select id="findByName" parameterType="string" resultMap="userMap"> select * from user where username like #{name} </select> <!-- 根据queryVo的条件查询用户 --> <select id="findUserByVo" parameterType="com.itheima.domain.QueryVo" resultMap="userMap"> select * from user where username like #{user.userName} </select> <!-- 根据条件查询 <select id="findUserByCondition" resultMap="userMap" parameterType="user"> select * from user where 1=1 <if test="userName != null"> and username = #{userName} </if> <if test="userSex != null"> and sex = #{userSex} </if> </select>--> <select id="findUserByCondition" resultMap="userMap" parameterType="user"> select * from user <where> <if test="userName != null"> and username = #{userName} </if> <if test="userSex != null"> and sex = #{userSex} </if> </where> </select> <!-- 根据queryvo中的Id集合实现查询用户列表 --> <select id="findUserInIds" resultMap="userMap" parameterType="queryvo"> <include refid="defaultUser"></include> <where> <if test="ids != null and ids.size()>0"> <foreach collection="ids" open="and id in (" close=")" item="uid" separator=","> #{uid} </foreach> </if> </where> </select> </mapper>
<?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> <!-- 配置properties--> <properties resource="jdbcConfig.properties"></properties> <!--使用typeAliases配置别名,它只能配置domain中类的别名 --> <typeAliases> <package name="com.itheima.domain"></package> </typeAliases> <!--配置环境--> <environments default="mysql"> <!-- 配置mysql的环境--> <environment id="mysql"> <!-- 配置事务 --> <transactionManager type="JDBC"></transactionManager> <!--配置连接池--> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments> <!-- 配置映射文件的位置 --> <mappers> <package name="com.itheima.dao"></package> </mappers> </configuration>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis jdbc.username=root jdbc.password=1234
package com.itheima.test; import com.itheima.dao.IUserDao; import com.itheima.domain.QueryVo; import com.itheima.domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * @author 黑马程序员 * @Company * * 测试mybatis的crud操作 */ public class MybatisTest { private InputStream in; private SqlSession sqlSession; private IUserDao userDao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ //1.读取配置文件,生成字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.获取SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.获取SqlSession对象 sqlSession = factory.openSession(true); //4.获取dao的代理对象 userDao = sqlSession.getMapper(IUserDao.class); } @After//用于在测试方法执行之后执行 public void destroy()throws Exception{ //提交事务 // sqlSession.commit(); //6.释放资源 sqlSession.close(); in.close(); } /** * 测试查询所有 */ @Test public void testFindAll(){ //5.执行查询所有方法 List<User> users = userDao.findAll(); for(User user : users){ System.out.println(user); } } /** * 测试删除操作 */ @Test public void testFindOne(){ //5.执行查询一个方法 User user = userDao.findById(50); System.out.println(user); } /** * 测试模糊查询操作 */ @Test public void testFindByName(){ //5.执行查询一个方法 List<User> users = userDao.findByName("%王%"); // List<User> users = userDao.findByName("王"); for(User user : users){ System.out.println(user); } } /** * 测试使用QueryVo作为查询条件 */ @Test public void testFindByVo() { QueryVo vo = new QueryVo(); User user = new User(); user.setUserName("%王%"); vo.setUser(user); //5.执行查询一个方法 List<User> users = userDao.findUserByVo(vo); for (User u : users) { System.out.println(u); } } /** * 测试查询所有 */ @Test public void testFindByCondition(){ User u = new User(); u.setUserName("老王"); // u.setUserSex("女"); //5.执行查询所有方法 List<User> users = userDao.findUserByCondition(u); for(User user : users){ System.out.println(user); } } /** * 测试foreach标签的使用 */ @Test public void testFindInIds(){ QueryVo vo = new QueryVo(); List<Integer> list = new ArrayList<Integer>(); list.add(41); list.add(42); list.add(46); vo.setIds(list); //5.执行查询所有方法 List<User> users = userDao.findUserInIds(vo); for(User user : users){ System.out.println(user); } } }