国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

MyBatis學習筆記(三)

2019-11-08 02:58:07
字體:
來源:轉載
供稿:網友

MyBatis學習筆記(三)

常用批量操作,SPRing與myBatis整合,簡化配置

個人筆記,如有錯誤,懇請批評指正。

常用批量操作

批量新增部門

映射文件定義SQL
<!-- 批量增加 --> <!-- insert into dept(dept_name,dept_address) values(name1,address1)(name2,address2)...(); --> <sql id="key"> <trim suffixOverrides=","> dept_name, dept_address, </trim> </sql> <insert id="insertDeptList"> insert into dept( <include refid="key"></include> ) values <foreach collection="list" item="dept" separator=","> (#{dept.deptName},#{dept.deptAddress}) </foreach> </insert>
編寫批量添加部門方法

數據操作類定義批量添加部門的方法

public int insertDeptList(List list){ int i = 0; try { session = MybatisSessionFactory.getSession(); i = session.insert("cn.ustb.entity.DeptMapper.insertDeptList", list); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally{ MybatisSessionFactory.closeSession(); } return i; }

批量刪除部門

映射文件定義SQL
<!-- 批量刪除 --> <delete id="deleteDeptList" parameterType="string"> delete from dept where dept_id in ( <foreach collection="array" item="id" separator=","> #{id} </foreach> ); </delete>
編寫批量刪除部門的方法
public int deleteDeptList(String[] ids){ int i = 0; try { session = MybatisSessionFactory.getSession(); i = session.delete("cn.ustb.entity.DeptMapper.deleteDeptList", ids); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally{ MybatisSessionFactory.closeSession(); } return i; }

批量修改員工信息

修改mybatis-config.xml文件
支持上有點麻煩,需要修改mybatis-config.xml文件相關數據庫連接的信息(主要url),以支持批量更新 <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.MySQL.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true" /> <property name="username" value="root"></property> <property name="passWord" value="root"></property> </dataSource> </environment> <environment id="test"> <transactionManager type=""/> <dataSource type=""></dataSource> </environment> </environments>
配置批量更新的sql
<!-- 批量更新 --> <update id="updateDeptList"> <foreach collection="list" item="dept" separator=";"> update dept <set> <if test="dept.deptName!=null">dept_name = #{dept.deptName},</if><!-- ','necessary --> <if test="dept.deptAddress!=null">dept_address = #{dept.deptAddress},</if> </set> where dept_id = #{dept.deptId} </foreach> </update>
編寫批量更新部門的方法
public int updateDeptList(List list){ int i = 0; try { session = MybatisSessionFactory.getSession(); i = session.update("cn.ustb.entity.DeptMapper.updateDeptList", list); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally{ MybatisSessionFactory.closeSession(); } return i; }

Spring+myBatis整合

準備工作

新建項目并導入jar包配置mybatis-config.xml創建庫及表創建實體編寫映射文件,修改mybatis-config.xml內容進行簡單測試(除了導入spring相關jar包外,以上內容可能直接使用上一章節內容)

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置數據源,記得去掉myBatis-config.xml的數據源相關配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <!-- 配置session工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatis-config.xml" /> <property name="mapperLocations" value="classpath:cn/ustb/entity/*.xml"/> </bean> <!-- 配置事務管理器,管理數據源事務處理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- deptDao的實現類對象 --> <bean id="deptDao" class="cn.ustb.dao.impl.DeptDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property> </bean> <!-- 配置SessionTemplate,已封裝了繁瑣的數據操作--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>

修改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> <typeAliases> <typeAlias type="cn.ustb.entity.Dept" alias="Dept" /> </typeAliases> <mappers> <mapper resource="cn/ustb/entity/DeptMapper.xml" /> </mappers></configuration>

編寫dao層接口及實現

DeptDao.java

public interface DeptDao { //根據部門ID查詢部門信息 public Dept selectOne(int deptId);}

修改接口實現類:DeptDaoImpl.java

public class DeptDaoImpl { private SqlSessionTemplate sqlSessionTemplate; public SqlSessionTemplate getSqlSessionTemplate() { return sqlSessionTemplate; } public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } public Dept selectOne(int deptId){ Dept dept = null; dept = sqlSessionTemplate.selectOne("cn.ustb.entity.DeptMapper.selectOne", deptId); return dept; }}

編寫業務層代碼 業務層接口略,這里只寫業務層實現類:DeptServiceImpl.java

public class DeptServiceImpl { private DeptDao deptDao; public DeptDao getDeptDao() { return deptDao; } @Resource public void setDeptDao(DeptDao deptDao) { this.deptDao = deptDao; } public Dept selectOne(int id){ return deptDao.selectOne(id); } public int insertDept(Dept dept){ return deptDao.insertDept(dept); }}

配置bean信息到sping配置文件

<!-- DAO層部門信息表的數據操作對象 --><bean id="deptDao" class="cn.ustb.dao.impl.DeptDaoImpl" > <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/></bean><!-- 業務層部門信息業務處理對象 --><bean id="deptService" class="cn.ustb.service.impl.DeptServiceImpl"> <property name="deptDao" ref="deptDao"/></bean>

簡化配置

掃描式加載SQL映射文件

修改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> <typeAliases> <typeAlias type="cn.ustb.entity.Dept" alias="Dept" /> </typeAliases> <!-- 采用掃描式加載映射文件,以下將不用配置,可以減少映射文件過多時維護的麻煩 --> <!-- <mappers> <mapper resource="cn/ustb/entity/DeptMapper.xml" /> </mappers> --></configuration>

修改applicationContext.xml,為SqlSessionFactoryBean設置mapperLocations屬性

<!-- 配置session工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:myBatis-config.xml" /> <!-- 配置掃描式加載SQL映射文件 --> <property name="mapperLocations" value="classpath:cn/ustb/entity/*.xml"/> </bean>

MapperScannerConfigurer簡化配置

1. 在spring配置文件中添加MapperScannerConfigurer 配置并去掉所有的Dao接口實現類配置
<!-- 配置 轉換器,對于在basePackage設置的包(包括子包)下的接口類的全類名和在Mapper.xml文件中定義過的命名空間一致, spring將會生成對應的代理對象(在調用 的地方通過@Autowired或者@Resource方式將可以注入接口實例)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="basePackage" value="cn.ustb.dao"/> </bean> <!-- DAO層部門信息表的數據操作對象,上面如果配置MapperScannerConfigurer轉換器,DAO接口將不再使用實現類 --> <!-- <bean id="deptDao" class="cn.ustb.dao.impl.DeptDaoImpl" > <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> --> <!-- 業務層部門信息業務處理對象 --> <bean id="deptService" class="cn.ustb.service.impl.DeptServiceImpl"> <!-- 上面如果配置MapperScannerConfigurer轉換器,DAO接口將不再使用實現類注入 --> <!-- <property name="deptDao" ref="deptDao"/> --> </bean>
2. 檢查或修改DeptMapper.xml文件

注意:命名空間+id和接口+方法名 一致

<?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"><!-- 這時的命名空間就需要和dao接口類全類名一致了 --><mapper namespace="cn.ustb.dao.DeptDao"> <resultMap id="deptResultMap" type="Dept"> <id property="deptId" column="dept_id" /> <result property="deptName" column="dept_name" /> <result property="deptAddress" column="dept_address" /> </resultMap> <!-- 這時的id就需要和dao接口的方法一致了 --> <select id="selectOne" parameterType="int" resultMap="deptResultMap"> select * from dept where dept_id=#{id} </select></mapper>
3. 業務類中,使用@Autowired為DAO接口注入對象public class DeptServiceImpl {
@Autowired private DeptDao deptDao;//省略其它代碼
4. 刪除Dao實現類(存在也沒有意義)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林周县| 逊克县| 维西| 汕头市| 宁陕县| 石阡县| 赣榆县| 高陵县| 蕉岭县| 天峨县| 玉树县| 灵丘县| 伊春市| 山阴县| 安福县| 苍梧县| 甘孜| 循化| 鄱阳县| 永登县| 皋兰县| 双牌县| 乃东县| 河津市| 喜德县| 剑河县| 韶山市| 九台市| 鄂托克旗| 满城县| 湘潭县| 和田市| 赣州市| 甘泉县| 延寿县| 福州市| 赞皇县| 贵南县| 苍梧县| 祁连县| 深水埗区|