POJO包装:完成综合查询,复杂查询,需要自定义包装类型的POJO
在POJO类型下新建UserQueryVo类:
VO:视图层的对象,表现层的东西(该包装对象能从试图层一致传下去)
宝:持久层的对象。
POJO:自定义的类似与PO,VO的综合体的的JavaBean中的
在新建一个用户扩展类UserCustom,逆向工程生成的东西我们最好不要去动它
一点小错误记录:
在map.xml文件中报这么个错
The content of element type "mapper" must match "(cache-ref|cache|resultMap*|parameterMap*|sql*|
insert*|update*|delete*|select*)+".出现这个问题 是因为<insert></insert>...........等标签写的不完整 或者写错位置了
比如<insert></insert>只写了一个,没有写结尾</insert>或者<insert></insert>中的</insert>写的位置不对,比如写在了代码里面
插播一个错误:mybatis查询数据,结果总是为null,总结如下:
1.数据库的字段名称和参数不对应
2.查询条件中带中文,或参数带中文,导致查询时与数据库中的编码不一致,查询结果为空,解决办法:
在jdbc.properties中,url后面加上?autoReconnect=true&useUnicode=true&characterEncoding=utf8,我的加的是
useUnicode=true&characterEncoding=utf-8,少加了autoReconnect=true,
附上正确的jdbc:
url:jdbc:mysql://127.0.0.1:3306/ssm?autoReconnect=true&useUnicode=true&characterEncoding=utf8
driver:com.mysql.jdbc.Driver
username:root
password:root
封装的查询类的数据格式:
所以其查询参数的格式为:
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
select * from user where name like '%${userCustom.name}%' and age=#{userCustom.age}
</select>
下面给出完整代码:‘
usermapper类:
public interface UserMapper {
public List<UserCustom> findUserList(UserQueryVo userQueryVo)throws Exception;
public User findUserById(int id)throws Exception;
public void insertUser(User user)throws Exception;
}
usermapper.xml:
<?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.lmj.ssm.mapper.UserMapper">
<!--用户信息的综合查询-->
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
select * from user where name like '%${userCustom.name}%' and age=#{userCustom.age}
</select>
</mapper>
User类不变。。。
userCustom类继承User类:
public class UserCustom extends User {
//扩展类,扩展用户
}
UserQueryVo:
public class UserQueryVo {
// //如果是这样写在外边,在mapper中的占位符里写${name}就ok
// ${userCustom.name}取出pojo包装对象中用户名称
//包装类
//在这里包装所需要的查询条件。
//用户查询条件
// private User user;
//可以包装其他的查询条件,例如:订单,商品
private UserCustom userCustom;
public UserCustom getUserCustom(){
return userCustom;
}
public void setUserCustom(UserCustom userCustom){
this.userCustom=userCustom;
}
}
db.properties:
url:jdbc:mysql://127.0.0.1:3306/ssm?autoReconnect=true&useUnicode=true&characterEncoding=utf8
driver:com.mysql.jdbc.Driver
username:root
password:root
mybatis-config.xml:
<?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 resource="db.properties"> </properties>
<typeAliases>
<!-- pojo类型的单个别名-->
<!--<typeAlias type="com.lmj.ssm.pojo.User" alias="User"/>-->
<!-- 命名空间的别名-->
<!-- <typeAlias type="com.lmj.ssm.mapper.UserMapper" alias="UserMapper"/>-->
<!-- <package name="com.lmj.ssm.mapper"/>-->
<!-- 包下的批量别名定义-->
<package name="com.lmj.ssm.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--通过resource单个加载-->
<!-- <mapper resource="mapper/UserMapper.xml"/>-->
<!-- 通过mapper接口单个加载
批量加载mapper-->
<!-- <mapper class="com.lmj.ssm.mapper.UserMapper"/> -->
<package name="com.lmj.ssm.mapper"/>
</mappers>
</configuration>
test类代码:
public class Test {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
public Test() throws IOException {
}
@org.junit.Test
public void findUserList() throws Exception {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//创建包装对象,设置查询条件
UserQueryVo userQueryVo=new UserQueryVo();
UserCustom userCustom=new UserCustom();
userCustom.setAge(24);
userCustom.setName("李");
userQueryVo.setUserCustom(userCustom);
List<UserCustom> user= userMapper.findUserList(userQueryVo);
System.out.println(user);
}
}