mybatis實(shí)現(xiàn)mapper代理模式的方式
今晚繼續(xù)復(fù)習(xí)mybtis以根據(jù)id值查詢單條數(shù)據(jù)為例編寫SqlMapConfig.xml文件
<configuration> <!-- 使用mybatis需要的數(shù)據(jù)源和事務(wù)配置,后續(xù)如果整合spring之后,將不再需要 --> <environments default='development'> <!-- 配置數(shù)據(jù)源和事務(wù) --> <environment id='development'> <!-- 配置事務(wù)管理,將事務(wù)管理交給mybatis管理 --> <transactionManager type='JDBC' /> <!-- 配置數(shù)據(jù)源 --> <dataSource type='POOLED'> <property name = 'driver' value = 'com.mysql.jdbc.Driver' /> <property name='url' value='jdbc:mysql://localhost:3306/db_shop? useUnicode=true&characterEncoding=utf-8'/> <property name='username' value='root'/> <property name='password' value=''/> </dataSource> </environment> </environments> <!-- 加載**.xml配置文件 --> <mappers> <mapper resource='product.xml'/></mappers> </configuration>
編寫失血模型對象:Product,最好字段名和數(shù)據(jù)庫里面的字段名一直
/**** <p>Title: Product</p> * <p>Description: 商品類失血模型</p> * @author Alon * @date 2020年9月27日 下午6:51:57 * @version 1.0 */ public class Product { private int p_id; private String name; private int p_number; private double price; private String add_time; public Product(int p_id, String name, int p_number, double price, String add_time) { super(); this.p_id = p_id; this.name = name; this.p_number = p_number; this.price = price; this.add_time = add_time; }public Product() { super(); } public int getP_id() {return p_id; } public void setP_id(int p_id) {this.p_id = p_id; }public String getName() { return name; } public void setName(String name) { this.name = name; } public int getP_number() {return p_number; } public void setP_number(int p_number) { this.p_number = p_number; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getAdd_time() { return add_time;}public void setAdd_time(String add_time) { this.add_time = add_time; }@Override public String toString() { return 'Product [p_id=' + p_id + ', name=' + name + ', p_number=' + p_number + ', price=' + price + ', add_time=' + add_time + ']'; } }
編寫單個(gè)映射關(guān)系的sql.xml:product.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'> <!-- 映射的sql文件 namespace:命名空間,可以理解成將部分的sql語句進(jìn)行隔離。到后面的mapper代理方式將有更 加重要的作用 --> <mapper namespace='test'> <!--select:表示要執(zhí)行的查詢語句 id:給這個(gè)查詢語句取一個(gè)名字,唯一的,java中要調(diào)用的使用。 parameterType:輸入?yún)?shù)的數(shù)據(jù)類型 resultType:輸出參數(shù)的數(shù)據(jù)類型,一般綁定成model的對象 #{value}:表示用來和占位符一樣,用來接受輸入的參數(shù)值。 --> <select parameterType='java.lang.Integer' resultType='com.woniuxy.model.Product'> SELECT * FROM t_product WHERE p_id = #{value} </select> </mapper>
編寫java代碼進(jìn)行測試執(zhí)行sql語句并得到結(jié)果
/**** <p>Title: MybatisDemo1</p> * <p>Description: 查詢數(shù)據(jù)的demo</p> * @author Alon * @date 2020年9月27日 下午7:05:32 * * @version 1.0 */ * public class MybatisDemo1 { * public static void main(String[] args) throws IOException { * new MybatisDemo1().queryById(); } * public void queryById() throws IOException { * //1、讀取到SqlMapConfig.xml文件的流 String path = 'SqlMapConfig.xml'; * InputStream config = Resources.getResourceAsStream(path);//創(chuàng)建會(huì)話工廠,同時(shí)將SqlMapConfig.xml里面的數(shù)據(jù)放到工廠中 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); //開啟會(huì)話 SqlSession sqlSession = factory.openSession(); /** 第一個(gè)參數(shù):要執(zhí)行哪個(gè)sql語句的參數(shù),一般命名空間.id值 * 第二個(gè)參數(shù):傳入填充#{value}的傳入?yún)?shù)值。 */ Object obj = sqlSession.selectOne('test.findById', 1); System.out.println(obj); } }
進(jìn)行模糊查詢:查詢出多條結(jié)果集
<!-- 在原有的xml文件中進(jìn)行配置即可,不需要新建,之前的也已經(jīng)在SqlMapConfig.xml文件中進(jìn)行了配 置 --> <select parameterType='java.lang.String' resultType='com.woniuxy.model.Product'> SELECT * FROM t_product WHERE name like '%${value}%' </select> public void queryByName() throws Exception{ String path = 'SqlMapConfig.xml'; //獲取流對象 InputStream config = Resources.getResourceAsStream(path); //獲取會(huì)話工廠 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); //開啟會(huì)話 SqlSession sqlSession = factory.openSession(); /** 因?yàn)閳?zhí)行結(jié)果有多條語句,那么必須使用selectList */ List<Object> list = sqlSession.selectList('test.queryByName', '旺仔'); //迭代 System.out.println(list); }
新增語句
<!-- 新增一條數(shù)據(jù) parameterType:傳入?yún)?shù)的數(shù)據(jù)類型,用失血模型對象即可。 --> <insert parameterType='com.woniuxy.model.Product'> INSERT INTO t_product(name,p_number,price) value(#{name},#{p_number},# {price}); </insert>
/****<p>Title: insert</p> *<p>Description: 新增數(shù)據(jù)</p> * @throws Exception * */ public void insert() throws Exception{ String path = 'SqlMapConfig.xml'; * //獲取流對象 * InputStream config = Resources.getResourceAsStream(path); //獲取會(huì)話工廠* SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); * //開啟會(huì)話 * SqlSession sqlSession = factory.openSession();* Product prod = new Product();* prod.setName('雪碧'); * prod.setP_number(10); * prod.setPrice(3.0);* int row = sqlSession.insert('test.insertProduct', prod); System.out.println(row); sqlSession.commit(); sqlSession.close(); }
mapper代理的方式進(jìn)行講解增、刪、改、查(重點(diǎn))步驟1:創(chuàng)建對應(yīng)的Mapper接口,和之前的dao接口一致步驟2:編寫對應(yīng)的xxmapper.xml配置文件。有要求1)mapper標(biāo)簽里面的namespace:必須寫成對應(yīng)的Mapper接口的全路徑名2)sql語句里面的id:必須和接口中對應(yīng)方法的名稱一致。3)sql語句里面的parameterType:必須要和接口中方法的形式參數(shù)數(shù)據(jù)類型一致4)sql語句里面的resultType:必須和接口中方法的返回值數(shù)據(jù)類型一致步驟3:將編寫好的**mapper.xml文件在SqlMapConfig.xml文件中加載步驟4:編寫實(shí)現(xiàn)類,并在這里直接測試別名和mapper映射詳解設(shè)置別名在mybatis配置文件中,別名是一直存在的,實(shí)際上在框架中已經(jīng)存在一些默認(rèn)的別名。比如java.lang.String對應(yīng)的別名:String,java.lang.Integer對應(yīng)的別名:int在開發(fā)中,一般mapper.xml配置文件中的parameterType和resultType往往對應(yīng)的名稱需要寫上model的全路徑,而且還是多次被重復(fù)使用,可以選擇給該路徑取一個(gè)別名。用別名來替換比較復(fù)雜的全路徑名以掃描包的形式進(jìn)行配置自定義的POJO類(重點(diǎn))自定義POJO類,一般指的是高級查詢,在想要的數(shù)據(jù)和傳入的條件不再一張表中,通過表所創(chuàng)建的失血模型model已經(jīng)不能夠滿足傳入?yún)?shù)的需求了,這個(gè)時(shí)候,就需要使用自定義的POJO類因?yàn)橐粋€(gè)失血模型已經(jīng)不能夠滿足查詢輸入?yún)?shù)的要求了,所以進(jìn)行擴(kuò)展,創(chuàng)建一個(gè)UserCustom類,里面包含user對象聲明和userInfo對象聲明
以上就是mybatis實(shí)現(xiàn)mapper代理模式的方式的詳細(xì)內(nèi)容,更多關(guān)于mybatis mapper代理模式的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 關(guān)于SQL server中字段值為null的查詢2. 提高商業(yè)智能環(huán)境中DB2查詢的性能(2)3. 關(guān)于Sql server數(shù)據(jù)庫日志滿的快速解決辦法4. SQL Server自動(dòng)生成日期加數(shù)字的序列號5. SQL Server數(shù)據(jù)庫連接查詢和子查詢實(shí)戰(zhàn)案例6. Access創(chuàng)建一個(gè)簡單MIS管理系統(tǒng)7. Microsoft Office Access凍結(jié)字段的方法8. SQL語句中的ON DUPLICATE KEY UPDATE使用9. How to access eclipse workspace?10. DB2 9(Viper)快速入門
