博客目录
(1)模糊查询的方法
(2)Mapper生命周期
(1)换一个别名
(2)、使用返回类型 resultMap
(1).通过UserMapper.xml配置来实现,UserMapper.xml配置:
(2).通过Java代码实现分页 (不建议使用,但是快)
(3)分页可以使用其他方法
(1).通过子查询来完成
(2).按照结果嵌套查询--联表查询
联表查询 小结
注意:采用了二级缓存会出现序列化的问题
缓存工作流程图
结论
1 public class SqlSessionFactoryUtil { 2 //创建SqlSessionFactory对象 3 private static SqlSessionFactory sqlSessionFactory =null; 4 //类线程死锁 5 private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class; 6 7 /** 8 * 私有化构造参数 9 */ 10 private SqlSessionFactoryUtil(){ 11 } 12 13 /** 14 * @return 15 * 单例模式,构造函数要私有化,并且需要用线程锁住,保证所有对象都使用一个SqlSessionFactory 16 */ 17 public static SqlSessionFactory initSqlSessionFactory() { 18 String resource ="mybatis-config.xml"; 19 InputStream inputStream = null; 20 try{ 21 inputStream = Resources.getResourceAsStream(resource); 22 }catch(IOException ex) { 23 Logger.getLogger(SqlSessionFactoryUtil.class.getName()).log(Level.SEVERE, 24 null,ex); 25 } 26 synchronized(CLASS_LOCK) { 27 if(sqlSessionFactory==null) { 28 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 29 } 30 } 31 return sqlSessionFactory; 32 } 33 34 public static SqlSession openSqlSession() { 35 if(sqlSessionFactory==null) { 36 initSqlSessionFactory(); 37 } 38 return sqlSessionFactory.openSession(); 39 } 40 }
UserMapper userMapper =session.getMapper(UserMapper.class); List<User> userList =userMapper.getList();
<!-- 模糊查询 需要:同时满足 3 个参数,不然失败,所以需要动态SQL方法--> <select id="selectByMap" parameterType="map" resultType="User"> select * from user where sex=#{sex} and address like concat("%",#{address},"%") and username like concat("%",#{username},"%d") </select> <!-- 改进版的模糊查询--> <select id="selectByMap1" parameterType="map" resultType="user"> select * from user <where> <if test="sex !=null and sex !=‘‘">sex=#{sex}</if> <if test="address !=null and address !=‘‘">address=#{address}</if> <if test="username !=null and username !=‘‘">username=#{username}</if> </where> </select>
<resultMap id="LimitType" type="User">
<result column="address" property="addes"/>
</resultMap>
<select id="getUserByLimit" parameterType="map" resultMap="LimitType">
select * from user limit #{startIndex},#{pageSize}
</select>
测试类:
@Test public void getUserByLimit(){ SqlSession sqlSession = SqlSessionFactoryUtil.openSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); Map map = new HashMap<String , Integer>(); map.put("startIndex", 0); map.put("pageSize",2); List <User> list=userMapper.getUserByLimit(map); for (User s :list) { System.out.println(s); } sqlSession.close(); }
RowBounds rowBoun RowBounds rowBounds= new RowBounds(0,5); List<User> list = sqlSession.selectList("com.ybzn.dao.UserMapper.getUserByLimit", null,rowBounds);
<resultMap id="LimitType" type="User">
<result column="address" property="addes"/>
</resultMap>
<select id="getUserByLimit" parameterType="map" resultMap="LimitType">
select * from user
</select>
(3)分页可以使用其他方法
<select id="getStudent" resultMap="StudentTeacher"> select * from new_user; </select> <resultMap id="StudentTeacher" type="Student"> <result column="id" property="id"></result> <result column="stuname" property="stuname"></result> <result column="id" property="id"></result> <!-- 复杂的属性 需要单独处理 对象:association 集合:collection --> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where tid=#{tid} </select>
<select id="getStudent2" resultMap="StudentTeacher2"> select s.id sid,s.stuname sname,s.stuclass sclass,t.teaname tname from new_user s,teacher t where s.tid=t.tid; </select> <resultMap id="StudentTeacher2" type="Student"> <result column="sid" property="id"></result> <result column="sname" property="stuname"></result> <result column="sclass" property="stuclass"></result> <association property="teacher" javaType="Teacher"> <result property="teaname" column="tname"></result> </association> //其中association 里面的property对应Pojo中的字段名 //javaType 对应pojo中的字段名类型 //配置中所有property 表示数据库中的字段名,Column表示程序中的别名或者字段名 </resultMap>
@Data public class Student { private int id; private String stuname; private String stuclass; private Teacher teacher; }
1 <!--按照结果来嵌套查询--> 2 <select id="getTeacher" resultMap="TeacherStudent"> 3 select s.id sid, s.stuname sname, s.stuclass sclass, t.teaname tname , t.tid tid 4 from new_user s , teacher t 5 where s.tid = t.tid and t.tid = #{tid}; 6 </select> 7 <resultMap id="TeacherStudent" type="Teacher"> 8 <result column="tname" property="teaname"></result> 9 <!-- 复杂的属性 需要单独处理 对象:association 集合:collection 10 javaType="" 指定属性的类型 11 集合中的泛型信息 ,我们可以通过ofType获取 12 --> 13 <collection property="student" ofType="Student"> 14 <result property="id" column="sid"></result> 15 <result property="stuname" column="sname"></result> 16 <result property="tid" column="tid"></result> 17 <result property="stuclass" column="sclass"></result> 18 </collection> 19 </resultMap>
@Data public class Teacher{ private int tid; private String teaname; private List<Student> student; }
<select id="queryBlogIF" parameterType="map" resultType="blog"> select * from blog <where> <if test="title != null"> and title = #{title} </if> <if test="author != null"> and author = #{author} </if> </where> </select>
<select id="queryBlogChoose" parameterType="map" resultType="blog"> select * from blog <where> <choose> <when test="title !=null"> title=#{title} </when> <when test="author != null"> and author=#{author} </when> <otherwise> and views=#{views} </otherwise> </choose> </where> </select>
<update id="updateBlog" parameterType="map"> update blog <set> <if test="title !=null"> title = #{title}, </if> <if test="author !=null "> author = #{author}, </if> </set> where id= #{id} </update>
<sql id="if-title-author"> <if test="title != null"> title = #{title} </if> <if test="author != null"> and author = #{author} </if> </sql>
<select id="queryBlogIf" parameterType="map" resultType="blog"> select * from blog <where> <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace --> <include refid="if-title-author"></include> <!-- 在这里还可以引用其他的 sql 片段 --> </where> </select>
<!-- 传递一个万能的map, 这个Map可以存在一个集合--> <select id="queryBlogForeach" parameterType="map" resultType="Blog"> select * from blog <where> <foreach collection="ids" item="flagid" open="and (" close=")" separator="or"> id=#{flagid} </foreach> </where> </select>
@Test public void sqlSelect(){ SqlSession session = SqlSessionFactoryUtil.openSqlSession(); BlogMapper mapper = session.getMapper(BlogMapper.class); HashMap map = new HashMap(); ArrayList <String> ids = new ArrayList <>(); ids.add("1"); ids.add("2"); ids.add("3"); ids.add("4"); map.put("ids", ids); List <Blog> blogs = mapper.queryBlogForeach(map); for (Blog blog : blogs) { System.out.println(blog); } }
<setting name="cacheEnabled" value="true"/>
<cache/> <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
public class Blog implements Serializable {}
原文:https://www.cnblogs.com/blogger-Li/p/12298299.html