最近整了整MyBatis+Spring整合开发,网上也搜了搜相关资料或者博客,对于初学者来说 肯定是希望有现成的项目直接拷贝导入到工程跑起来就能看到效果。 建议即便是有现成项目,初学者一定要把配置运行的整个过程自己走一遍整个工程并不复杂 简单的CRUD 整个工程结构图如下:
仍然采取三层架构模式进行 好了 直接上代码
这里我注释掉了sqlSessionTemplate配置,下面在daoImpl中继承SqlSessionDaoSupport,可直接注入sqlSessionFactory
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 这个地方开始放置所有bean的配置信息 --> <!--配置jdbc.properties文件的位置信息,路径还是区分大小写 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties" /> </bean> <!-- 读取jdbc.properties文件,配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> <!-- 数据库连接池配置 --> <!-- 初始化连接数量 --> <property name="initialSize" value="60" /> <!-- 最大连接数量 --> <property name="maxActive" value="100" /> <!-- 最大空闲连接数量 --> <property name="maxIdle" value="50" /> <!-- 最小空闲连接数量 --> <property name="minIdle" value="10" /> </bean> <!-- sqlSessionFactory --> <!-- MyBatis在spring中Bean的配置,都是固定的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置SqlSessionTemplate <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> --> <!--事务相关控制--> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务传播特性 --> <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/> <tx:method name="doAdd*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" /> <tx:method name="doEdit*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="find*" propagation="SUPPORTS"/> <tx:method name="get*" propagation="SUPPORTS"/> <tx:method name="select*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!-- 配置AOP --> <aop:config> <aop:pointcut id="pc" expression="execution(public * com.mybatis.biz.*.*(..))" /> <!--把事务控制在Service层--> <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> </aop:config> <!--配置dao层 --> <bean id="userDao" class="com.mybatis.daoImpl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!--配置biz层 --> <bean id="userBiz" class="com.mybatis.bizImpl.UserBizImpl"> <property name="userDao" ref="userDao" /> </bean> <!--配置action层 --> <bean id="userAction" class="com.mybatis.action.UserAction"> <property name="userBiz" ref="userBiz" /> </bean> </beans>
driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/mybatis user=root password=123456
<?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> <!-- 为一个vo类取一个别名儿,后面用的时候可以直接用该vo类的别名 而不用在写 包+类名称 --> <typeAlias type="com.mybatis.entity.UserInfo" alias="UserInfo" /> </typeAliases> <mappers> <!-- 对应vo类的DAO接口 所对应的xml配置文件的路径信息 --> <mapper resource="com/mybatis/entity/UserInfo.xml" /> </mappers> </configuration>
package com.mybatis.entity; public class UserInfo { private int id; private String name; private int age; private String sdesc; public UserInfo() { super(); } public UserInfo(int id,String name, int age, String sdesc) { super(); this.id = id; this.name = name; this.age = age; this.sdesc = sdesc; } public UserInfo(String name, int age, String sdesc) { super(); this.name = name; this.age = age; this.sdesc = sdesc; } //stters and getters }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!--namespace的作用是为这张表取一个名字,好在持久化层中进行访问 --> <mapper namespace="UserInfo"> <!-- 插入一行数据 --> <insert id="doAdd" parameterType="UserInfo" keyProperty="id"> INSERT INTO userInfo(name,age,sdesc) values(#{name},#{age},#{sdesc}) </insert> <!-- 删除一条数据 --> <delete id="delete" parameterType="int"> DELETE FROM userInfo WHERE id =#{id} </delete> <!-- 更新一条数据 --> <update id="update" parameterType="UserInfo"> UPDATE userInfo SET name=#{name},age=#{age}, sdesc=#{sdesc} WHERE id=#{id}; </update> <!-- 查询该表的总行数 --> <select id="getCount" resultType="int"> SELECT COUNT(*)FROM userInfo WHERE id='0' </select> <!-- 查询一条数据 --> <select id="getById" parameterType="int" resultType="UserInfo"> SELECT id,name,age,desc FROM userInfo WHERE id=#{id} </select> <!-- 查询一组数据 --> <select id="findAll" resultType="UserInfo" > SELECT * FROM userInfo </select> </mapper>
注意:我这里使用持久化层继承 SqlSessionDaoSupport,和hibernate一个模式,这里继承之后我们在spring配置文件里面就可以直接给dao层注入sqlSessionFactory,它会帮我们生成我们需要的sqlSession调用为我们封装好的各种方法。
package com.mybatis.daoImpl; import java.util.List; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.mybatis.dao.UserDao; import com.mybatis.entity.UserInfo; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { /** * 插入数据 */ public void doAdd(UserInfo userinfo) { this.getSqlSession().insert("UserInfo.doAdd", userinfo); } /** * 删除 */ public void delete(int id) { this.getSqlSession().delete("UserInfo.delete", id); } /** * 修改 */ public void doEdit(UserInfo userinfo) { this.getSqlSession().update("UserInfo.update", userinfo); } /** * 查询所有 */ public List<UserInfo> findAll() { return this.getSqlSession().selectList("UserInfo.findAll"); } }
所需jar包 见附件