编写UserDao接口
public interface UserDao {
List<User> findAll() throws IOException;
}
编写UserDaoImpl实现
public class UserDaoImpl implements UserDao {
public List<User> findAll() throws IOException{
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("userMapper.findAll");
sqlSession.close();
return userList;
}
}
测试手动对dao进行实现
@Test
public void testTraditionDao() throws IOException {
//手动对dao进行实现
UserDao userDao = new UserDaoImpl();
List<User> all = userDao.findAll();
System.out.println(all);
}
代理开发介绍:
采用Mybatis的代理开发实现DAO层的开发,这种方式是我们后面进入企业的主流。
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由mybatis框架根据定义接口创建接口的动态代理对象,代理对象的方法体同上边dao接口实现类方法。
Mapper 接口开发需要遵循以下规范:
1、 Mapper.xml
文件中的namespace
与mapper接口的全限定名相同
2、Mapper接口方法名
和Mapper.xml
中定义的每个statement的id相同
3、Mapper接口方法
的输入参数类型
和mapper.xml中定义的每个sql的parameterType
的类型相同
4、 Mapper接口方法
的输出参数类型
和mapper.xml中定义的每个sql的resultType
的类型相同
测试代理dao
public class UserService {
public static void main(String[] args) throws IOException {
//测试代理dao
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获得MyBatis框架生成的UserMapper接口的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById(1);
System.out.println(user);
sqlSession.close();
}
}
Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化的此时在前面的学习中我们的 SQL 就不能满足要求了。
参考的官方文档,描述如下
我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。比如在 id如果不为空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
<select id="findByCondition" parameterType="user" resultType="user">
select * from User
<where>
<if test="id!=0">
and id=#{id}
if> <if test="username!=null">
and username=#{username}
if> <if test="password!=null">
and password=#{password}
if>
where>
select>
当查询条件id和username都存在时,控制台打印的sql语句如下:
public class UserService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User condition = new User();
condition.setId(1);
condition.setUsername("[email protected]");
//condition.setPassword("123456");
User user = userMapper.findByCondition(condition);
System.out.println(user);
sqlSession.close();
}
}
循环执行sql的拼接操作,例如: select *from user where id in (1,2,5)
<select id="findByIds" parameterType="list" resultType="user">
select * from User
<where>
<foreach collection="list" open="id in (" close=")" item="id" separator=",">
#{id}
foreach>
where>
select>
public class UserService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// User condition = new User();
//
// condition.setId(1);
// condition.setUsername("[email protected]");
// //condition.setPassword("123456");
//
// User user = userMapper.findByCondition(condition);
//
// System.out.println(user);
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
List<User> userList = userMapper.findByIds(ids);
System.out.println(userList);
sqlSession.close();
}
}
<sql id="selectUser">selecet * from usersql>
<select id="findByCondition" parameterType="user" resultType="user">
<include refid="selectUser">include>
<where>
<if test="id!=0">
and id=#{id}
if> <if test="username!=null">
and username=#{username}
if> <if test="password!=null">
and password=#{password}
if>
where>
select>
<select id="findByIds" parameterType="list" resultType="user">
<include refid="selectUser">include>
<where>
<foreach collection="list" open="id in (" close=")" item="id" separator=",">
#{id}
foreach>
where>
select>