Mybatis基础学习3——Dao层的封装
传统的Dao层的封装
map层的配置
<select id="getUserByid" parameterType="int" resultType="com.hhh.spring.demo.bean.User" >
SELECT * FROM USER WHERE id = #{id1}
</select>
<select id="getUserByName" parameterType="string" resultType="com.hhh.spring.demo.bean.User">
<!-- SELECT * FROM USER WHERE username LIKE #{name} -->
SELECT * FROM USER WHERE username LIKE '%${value}%'
</select>
<insert id="insertUser" parameterType="com.hhh.spring.demo.bean.User" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="uuid2" resultType="String" order="BEFORE">
SELECT UUID()
</selectKey>
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`,
`uuid2`)
VALUES (#{username},
#{birthday},
#{sex},
#{address},
#{uuid2});
</insert>
UserDao接口和UserDaoImpl层
public interface UserDao {
User getUserById(int id);
List<User> getUserByUserName(String userName);
void insertUser(User user);
}
public class UserDaoImpl implements UserDao {
@Override
public User getUserById(int id) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行删除
User mUser = sqlSession.selectOne("user.getUserByid", 31);
// 释放对象
sqlSession.close();
return mUser;
}
@Override
public List<User> getUserByUserName(String userName) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行删除
List<User> userList = sqlSession.selectList("user.getUserByName", userName);
sqlSession.close();
return userList;
}
@Override
public void insertUser(User user) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行删除
sqlSession.insert("user.insertUser", user);
sqlSession.commit();
sqlSession.close();
}
}
应用层
@Test
public void demo1() throws Exception {
UserDao userDao = new UserDaoImpl();
User mUser = userDao.getUserById(31);
System.out.println(mUser);
}
@Test
public void demo2() throws Exception {
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.getUserByUserName("张");
System.out.println(userList);
}
@Test
public void demo3() throws Exception {
UserDao userDao = new UserDaoImpl();
User user = new User();
user.setUsername("王麻子");
user.setSex("1");
user.setBirthday(new Date());
user.setAddress("深圳黑马");
userDao.insertUser(user);
}
动态代理开发
规则:
1、nameSpace必须是全路径名
2、接口的方法名必须与sql id一致。
3、接口的入参必须与paramsType一致
4、接口的返回值必须与resultType一致
代码使用
<!--命名空间,用于隔离sql语句-->
<mapper namespace="com.hhh.spring.demo.mapper.UserMapper">
<!--id: sql 中的id,语句中的位移标识-->
<!--resultType: 返回转化的对象-->
<!--#{} 点位符,相当于jdbc的?-->
<select id="getUserById" parameterType="int" resultType="com.hhh.spring.demo.bean.User" >
SELECT * FROM USER WHERE id = #{id1}
</select>
<select id="getUserByName" parameterType="string" resultType="com.hhh.spring.demo.bean.User">
<!-- SELECT * FROM USER WHERE username LIKE #{name} -->
SELECT * FROM USER WHERE username LIKE '%${value}%'
</select>
<insert id="insertUser" parameterType="com.hhh.spring.demo.bean.User" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="uuid2" resultType="String" order="BEFORE">
SELECT UUID()
</selectKey>
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`,
`uuid2`)
VALUES (#{username},
#{birthday},
#{sex},
#{address},
#{uuid2});
</insert>
</mapper>
通过mapper进行方法调用
@Override
public User getUserById(int id) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取映射对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 执行命令
User mUser = mapper.getUserById(id);
// 释放对象
sqlSession.close();
return mUser;
}
@Override
public List<User> getUserByUserName(String userName) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取映射对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 执行命令
List<User> userByUserName = mapper.getUserByUserName(userName);
// 释放对象
sqlSession.close();
return userByUserName;
}
@Override
public void insertUser(User user) {
SqlSessionFactory sqlSessionFactory = SqlSessionnFactoryUtils.getSqlSessionFactory();
// 创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取映射对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 执行命令
mapper.insertUser(user);
// 执行删除
sqlSession.commit();
sqlSession.close();
}