当前位置:Gxlcms > 数据库问题 > mybatis 动态sql

mybatis 动态sql

时间:2021-07-01 10:21:17 帮助过:9人阅读

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.etc.dao.HouseDAO">
 4 
 5    <!--查询单个条件 相当于switch=choose  when=case otherwise=default-->
 6     <select id="searchSim" resultType="house">
 7         select * from t_house
 8         <where>
 9             <choose>
10                 <when test="title!=null">
11                     title like ‘%${title}%‘
12                 </when>
13                 <when test="price!=null">
14                     price=#{price}
15                 </when>
16                 <otherwise>
17                     1=1
18                 </otherwise>
19             </choose>
20         </where>
21     </select>
22     <!--查询多个条件 if 如果存在就拼接
23     select * from t_house where title like ‘%?%‘ and price=?
24     -->
25     <select id="searchOdd" resultType="house">
26         select * from t_house
27         <where>
28             <if test="title!=null">
29                 title like ‘%${title}%‘
30             </if>
31             <if test="price!=null">
32                 and price=#{price}
33             </if>
34         </where>
35     </select>
36     <!--查询in collection集合名 item:参数名 open close=() separator用逗号拼接
37                 select * from t_house where id in (1,2,3)
38     -->
39     <select id="searchByIds" resultType="house">
40         select * from t_house where id in
41         <foreach collection="ids" item="id" open="(" close=")" separator=",">
42             #{id}
43         </foreach>
44     </select>
45     <!--更新(只更新存在的值)-->
46     <update id="update">
47         update t_house
48         <set>
49             <if test="title!=null">
50                 title=#{title},
51             </if>
52             <if test="price!=null">
53                 price=#{price}
54             </if>
55         </set>
56         where id =#{id}
57     </update>
58 
59 </mapper>

com.etc.dao.HouseDAO:
 1 package com.etc.dao;
 2 
 3 import com.etc.entity.House;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 
 8 public interface HouseDAO {
 9 
10     //查询单个条件 利用switch
11     List<House> searchSim(House house);
12     //查询多个条件 利用if
13     List<House> searchOdd(House house);
14     //查询in
15     List<House> searchByIds(@Param("ids") List<Integer> ids);
16     //更新(只更新存在的值)
17     void update(House house);
18 }

 

mybatis-config.xml:

技术图片
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC
 3         "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 别名 -->
 8     <typeAliases>
 9         <package name="com.etc.entity"></package>
10     </typeAliases>
11 
12     <!-- 配置环境变量 -->
13     <!-- 开发 测试  预生产 生产 -->
14     <environments default="development">
15         <environment id="development">
16             <transactionManager type="JDBC"/>
17             <dataSource type="POOLED">
18                 <property name="driver" value="com.mysql.jdbc.Driver"/>
19                 <property name="url"
20                           value="jdbc:mysql://127.0.0.1:3310/mybatis"/>
21                 <property name="username" value="root"/>
22                 <property name="password" value="123456"/>
23             </dataSource>
24         </environment>
25     </environments>
26 
27 
28     <!-- 配置mappers -->
29     <mappers>
30         <mapper resource="HouseDAO.xml"></mapper>
31     </mappers>
32 
33 </configuration>
View Code

 

com.etc.dao.HouseTest:

技术图片
 1 package com.etc.dao;
 2 
 3 import com.etc.entity.House;
 4 import org.apache.ibatis.io.Resources;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 8 import org.junit.Test;
 9 
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.List;
13 
14 public class HouseTest {
15 
16     @Test
17     public void test() throws IOException {
18         //加载配置文件 会话工厂
19         InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
20         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
21 
22         //会话   ==相当于数据库连接
23         SqlSession session=sqlSessionFactory.openSession();
24 
25         HouseDAO houseDAO=session.getMapper(HouseDAO.class);
26 
27         House house=new House();
28 //        house.setTitle("he");
29 //        house.setPrice(1300.0);
30 //        house.setId(1);
31 
32 //        List<House> search=houseDAO.searchSim(house);
33         List<House> search=houseDAO.searchOdd(house);
34 //        List<House> search=houseDAO.searchByIds(Arrays.asList(1,2,3));
35 //        houseDAO.update(house);
36         for (House h:search)
37             System.out.println(h);
38 
39 
40 
41         session.commit();
42         session.close();
43     }
44 }
View Code

 

mybatis 动态sql

标签:环境   manage   ram   数据库连接   drive   ons   功能   别名   sed   

人气教程排行