?
準備數據
create table d_user( id int primary key auto_increment, name varchar(10), age int(3) ); insert into d_user(name,age) values('Tom',12); insert into d_user(name,age) values('Bob',13); insert into d_user(name,age) values('Jack',18);
?
與數據庫表對應的javabean
package com.hb.bean; public class User { private int id; private String name; private int age; private int minAge; private int maxAge; public int getMinAge() { return minAge; } public void setMinAge(int minAge) { this.minAge = minAge; } public int getMaxAge() { return maxAge; } public void setMaxAge(int maxAge) { this.maxAge = maxAge; } //get,set方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public User() { } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
備注:javabean比user表多了minAge喝maxAge兩個屬性,用于數據篩選。?
?
數據庫表與javabean的映射文件
<?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.hb.bean.userMapper"> <!-- 這里namespace必須是“包名 + xml文件名”,不然要運行的時候要報錯 “is not known to the MapperRegistry” --> <select id="getUser" parameterType="com.hb.bean.User" resultType="com.hb.bean.User"> select * from user where age>=#{minAge} and age<=#{maxAge} <if test='name!=null'>and name like #{name}</if> </select> </mapper>
?
測試類
package com.hb.test5; import java.io.InputStream; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import com.hb.bean.User; public class Test5 { public static SqlSessionFactory sqlSessionFactory; public static SqlSession sqlSession; @Before public void init(){ //"/"表示是在src根目錄下 String source = "config.xml"; InputStream inputStream = Test5.class.getClassLoader().getResourceAsStream(source); try { sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // sqlSession = sqlSessionFactory.openSession(); //是否自動提交,默認不是自動提交的 sqlSession = sqlSessionFactory.openSession(true); } catch (Exception e) { e.printStackTrace(); } } @Test public void selectByCondition(){ String statment = "com.hb.bean.userMapper.getUser"; User conditon = new User(); conditon.setMaxAge(15); conditon.setMinAge(1); // conditon.setName("%a%"); //查詢list集合 List<User> users = sqlSession.selectList(statment, conditon); System.out.println(users); } }
?
測試打印的日志
?
2015-02-24 11:54:42,824 [main] DEBUG [com.hb.bean.userMapper.getUser] - ooo Using Connection [com.mysql.jdbc.Connection@11fda7c] 2015-02-24 11:54:42,824 [main] DEBUG [com.hb.bean.userMapper.getUser] - ==> Preparing: select * from user where age>=? and age<=? 2015-02-24 11:54:42,887 [main] DEBUG [com.hb.bean.userMapper.getUser] - ==> Parameters: 1(Integer), 15(Integer) [User [id=8, name=Tom, age=12], User [id=9, name=Bob, age=13]]
?
mybatis的配置文件
<?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> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <environments default="development"> <environment id="development"> <transactionManager type="jdbc" /> <dataSource type="POOLED"> <!-- <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@192.168.56.201:1521:system"/> --> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hb/bean/userMapper.xml" /> </mappers> </configuration>
?
?下面是動態配置SQL的實例
<!-- 分頁通過業務查詢所有的機構 --> <select id="queryOrgByApp" parameterType="java.util.Map" resultType="adtec.ta_org_relationManager.model.Ta_org_relation"> select a.orgRelationId,a.appid,a.ta_id,a.orgId,c.orgName,b.appName from ta_org_relation a,ta_app b,ta_organization c where a.appid = b.appid and a.orgId = c.orgId and a.appid=#{appid} <if test="orgId !=null"> and a.orgId in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </if> limit #{start},#{pageSize} </select>
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
