Mapper代理的输入映射和输出映射
来源:互联网 发布:js中window和document 编辑:程序博客网 时间:2024/05/22 15:18
输入映射类型:
简单类型
1映射文件
<!-- 根据用户ID查询用户信息 --><select id="findUserById" parameterType="int" resultType="cn.itcast.po.User">select * from user where id = #{id}</select>
2Mapper接口
//根据用户ID查询用户信息 public User findUserById(int id) throws Exception;
3测试
public class UserMapperTest {private SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception {//读取配置文件String resource ="SqlMapConfig.xml";InputStream inputStream = Resources.getResourceAsStream(resource);//创建SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testFindUserById() throws Exception {//创建UserMapper对象SqlSession sqlSession = sqlSessionFactory.openSession();//由mybatis通过Sqlsession创建代理对象UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.findUserById(1) ;System.out.println(user);sqlSession.close();}}
pojo类型
1映射文件
<!-- 添加用户 --><insert id="insertUser" parameterType="cn.itcast.po.User">insert into User(username, birthday, sex, address) values(#{username}, #{birthday},#{sex},#{address})</insert>
2Mapper接口
//添加用户 public void insertUser(User user);
3测试
@Testpublic void testInsertUser() {//创建UserMapper对象SqlSession sqlSession = sqlSessionFactory.openSession(true) ;UserMapper userMapper = sqlSession.getMapper(UserMapper.class) ;User user = new User() ;user.setUsername("znzn");user.setAddress("hubei");userMapper.insertUser(user);sqlSession.close();}
包装类型
1定义包装pojo类型类
package cn.itcast.po;
import java.util.HashMap;
import java.util.Map;
public class UserCustom {
private User user ;
Map<String, Object> map = new HashMap<String, Object>() ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
2映射文件
<select id="findUserList" parameterType="cn.itcast.po.UserCustom" resultType="cn.itcast.po.User">select * from user whereusername like '%${user.username}%'and sex = #{user.sex}</select>
3Mapper接口
public List<User> findUserList(UserCustom user) ;
4测试
@Testpublic void testFindUserList() {//创建UserMapper对象SqlSession sqlSession = sqlSessionFactory.openSession(true) ;UserMapper userMapper = sqlSession.getMapper(UserMapper.class) ;UserCustom userCustom = new UserCustom();User user = new User() ;user.setSex("1");user.setUsername("小明");userCustom.setUser(user);List<User> list = userMapper.findUserList(userCustom);int count = userMapper.findUserCount(userCustom);System.out.println(list);System.out.println(count);sqlSession.close();}
map或list类型
1映射文件
<select id="findUserByIdList" parameterType="java.util.List" resultType="user">select * from user<where><if test="list != null and list.size() > 0"><foreach collection="list" item="id" open="And id IN(" close=")" separator=",">#{id}</foreach></if></where></select>
2UserMapper接口
public List<User> findUserByIdList(List<Integer> idList);
3测试
@Testpublic void testFindUserByIdList(){SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<Integer> idList = new ArrayList<Integer>();idList.add(1);idList.add(10);idList.add(16);List<User> list = userMapper.findUserByIdList(idList);System.out.println(list);}
输出类型
分两种 resultType 和 resultMap
resultType简单类型 pojo类型
简单类型
1映射文件
<select id="findUserCount" parameterType="cn.itcast.po.UserCustom" resultType="int">select count(*) from user whereusername like '%${user.username}%'and sex = #{user.sex}</select>
pojo类型
<!-- 根据用户ID查询用户信息 --><select id="findUserById" parameterType="int" resultType="cn.itcast.po.User">select * from user where id = #{id}</select>
resultMap pojo类型
1映射文件
<!-- resultMap入门 id标签 专门为查询结果中唯一列映射 result映射查询结果中的普通列 --><resultMap type="cn.itcast.po.User" id="UserRstMap"><id column="id_" property="id" /><result column="username_" property="username"/><result column="sex_" property="sex" /></resultMap><select id="findUserRstMap" parameterType="int" resultMap="UserRstMap">select id id_, username username_, sex sex_ from user where id = #{id}</select>
2Mapper接口
public User findUserRstMap(int id) ;
3测试
@Testpublic void testFindUserRstMap() {//创建UserMapper对象SqlSession sqlSession = sqlSessionFactory.openSession(true) ;UserMapper userMapper = sqlSession.getMapper(UserMapper.class) ;User user = userMapper.findUserRstMap(1);System.out.println(user);sqlSession.close();}
阅读全文
0 0
- Mapper代理的输入映射和输出映射
- Mybatis学习(05)-mapper代理方法开发dao && 输入映射和输出映射
- mybatis进阶--mapper输入映射和输出映射
- 输入映射和输出映射
- mybatis的输入和输出映射
- mybatis 输入映射和输出映射
- mybatis进阶--输入映射和输出映射
- 【MyBatis】输入映射和输出映射
- Mybatis 输入映射和输出映射
- MyBatis中输入映射和输出映射
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- Mybatis 输入和输出映射
- Mybatis 输入映射 输出映射 高级映射
- Spring-Mybatis扫描jar包里面mapper代理对象和的映射文件
- mybatis入门基础(四)----输入映射和输出映射
- 【MyBatis学习06】输入映射和输出映射
- mybatis入门基础(四)----输入映射和输出映射
- mybatis入门基础(四)----输入映射和输出映射
- CSS 6种完全居中最佳实践(整理)
- <读书笔记>JavaScript系列之7种创建对象(面向对象)
- <读书笔记>《Web前端开发最佳实践》
- <读书笔记>《React:引领未来的用户界面开发框架》
- BlackJack(21) on GitHub
- Mapper代理的输入映射和输出映射
- [转]Hadoop之HDFS
- 重温《javascript高级程序设计》(第3版)
- note《JavaScript 秘密花园》
- display:none与visible:hidden
- Maven的排除依赖、归类依赖、优化依赖
- 置换元素
- WinDbg 命令三部曲:(二)WinDbg SOS 扩展命令手
- 《非暴力沟通》读书感悟