1. Input Parameters
Reference to the previous blog: https://blog.csdn.net/weixin_44212815/article/details/92859135
简单类型: 基本数据类型+ String类型
#{ } :名称随便
${ } :${value}
pojo 类型
#{ } 、${ } : 属性名引用
包装对象类型
引用 #{属性.属性名}
Map集合
引用: #{key}
多个参数
引用时:#{param1},#{param2}.....#{paramN}
1.1 Input parameters for the wrapper class object (a property of the class is another Pojo Pojo a)
Why wrapper object?
In the usual development, not necessarily for a simple CRUD entity, such as the completion of the comprehensive inquiry user information, and sometimes we need to pass the query is complex and may include other user information, and other related table. In response to this demand, in Mybatis we can use a custom packaging type of Pojo, Pojo in the package type of packaging into complex query conditions.
User class is below normal
public class User {
private Integer id;
private String username;
private String password;
private String sex;
private String address;
private String birthday;
省略set、get方法...
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
The following is a class packaging, as the User Attribute
public class QueryVO {
private User user;
private Integer startIndex;
private Integer pageSize;
省略set、get方法
UserDao mapping configuration file
<select id="findByQueryVO" parameterType="queryvo" resultType="user">
select * from user where username like "%"#{user.username}"%" limit #{startIndex},#{pageSize}
</select>
UserDao Interface
/**
* 多条件查询
* @param queryVO
* @return
*/
public List<User> findByQueryVO(QueryVO queryVO);
This will do the mapping wrapper class object, perform the following tests:
public class TestMybatisProxy {
SqlSessionFactory sessionFactory;
@Before
public void init(){
sessionFactory = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
}
@Test
public void testFindQueryVO(){
SqlSession sqlSession = sessionFactory.openSession();
//获取Dao接口的动态代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//创建QueryVo对象
QueryVO queryVO = new QueryVO();
User user = new User();
user.setUsername("小明");
queryVO.setUser(user);
queryVO.setStartIndex(0);
queryVO.setPageSize(2);
List<User> userList = userDao.findByQueryVO(queryVO);
for (User u : userList) {
System.out.println(u);
}
sqlSession.close();
}
}
A set of input parameters Map 1.2
UserDao Interface
/**
* 多条件查询
* @return
*/
public List<User> findByMap(Map<String,Object> map);
UserDao interface map
<select id="findByMap" parameterType="map" resultType="user">
select * from user where username like "%"#{username}"%" limit #{startIndex},#{pageSize}
</select>
test program
@Test
public void testFindByMap(){
SqlSession sqlSession = sessionFactory.openSession();
//获取Dao接口的动态代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
// 创建map对象
Map<String,Object> map = new HashMap<>();
map.put("username","小明");
map.put("startIndex",0);
map.put("pageSize",2);
List<User> userList = userDao.findByMap(map);
for (User u : userList) {
System.out.println(u);
}
sqlSession.close();
}
1.3 a plurality of input parameters Parameter
UserDao Interface
/**
* 根据多条件查询
* @param username
* @param startIndex
* @param pageSize
* @return
*/
public List<User> findByManyParam(String username ,Integer startIndex ,Integer pageSize);
UserDao interface map
<select id="findByManyParam" resultType="user">
select * from user where username like "%"#{param1}"%" limit #{param2},#{param3}
</select>
test program:
@Test
public void testFindByManyParam(){
SqlSession sqlSession = sessionFactory.openSession();
//获取Dao接口的动态代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.findByManyParam("小明", 0 ,2);
for (User u : userList) {
System.out.println(u);
}
sqlSession.close();
}
2. Return Value Type
By the time resultType output mapping, check out the required database column names and pojo the corresponding attribute name to be consistent before they can do the right map, and if not will be mapping errors, inconsistent use resultMap do output mapping
UserDao.xml
in the configuration file to add:
<resultMap id="userList" type="user">
<id property="id" column="uid"></id>
<result property="username" column="uname"></result>
</resultMap>
On top of several attributes:
-
id: Indicates the unique identifier of resultMap
-
type: resultMap represents the final mapping of java object type, use of the above is an alias, refer to the final configuration file to add a blog content
Mapping a single type
<typeAlias type = "com.itheima.domain.User" alias = "user"> </ typeAlias>
If not set too alias, type to use fully qualified class name -
<Id>: represents a primary key to uniquely identify the result set
-
<Result>: result of the normal field mapping focus, the mapping is a field other than the primary key.
-
column: the name of the database column
-
property: pojo property name
<select id="findAll" resultMap="userList">
select * from user
</select>
UserDao Interface
public interface UserDao {
/**
* 查全部
* @return
*/
public List<User> findAll();
}
test program
public class TestMybatisProxy {
SqlSessionFactory sessionFactory;
@Before
public void init(){
sessionFactory = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
}
@Test
public void testFindAll(){
SqlSession sqlSession = sessionFactory.openSession();
//获取Dao接口的动态代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}