spring+mybatis 框架搭建
注意<!-- 中间的字要保持与左右留出一个空格,否则会报错说出现两杠线 -->
1.导入jar包
aopalliance-1.0.jar
asm-3.3.1.jar
cglib-2.2.2.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
junit-4.9.jar
log4j-1.2.17.jar
mybatis-3.0.3.jar
mybatis-3.2.2.jar
mybatis-spring-1.0.0-RC3.jar
mysql-connector-java-5.1.10-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
2.写
src
----cn.itcast.mybatis.damain
User.java
User.xml
----cn.itcast.mybatis.dao
IUserDao.java
UserDaoImpl.java
----cn.itcast.mybatis.service
IUserService
UserServiceImpl
----spring-beans.xml
----sqlMapConfig.xml
User.java
1 package cn.itcast.mybatis.domain; 2 3 public class User { 4 private String id; 5 private String name; 6 private Integer age; 7 private String address; 8 public String getAddress() { 9 return address; 10 } 11 public void setAddress(String address) { 12 this.address = address; 13 } 14 public String getId() { 15 return id; 16 } 17 public void setId(String id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public Integer getAge() { 27 return age; 28 } 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 @Override 33 public String toString() { 34 return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; 35 } 36 37 }
User.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="cn.itcast.mybatis.domain.User"> 7 <sql id="cols"> 8 id,name,age,address 9 </sql> 10 <sql id="ucols"> 11 name=#{name},age=#{age},address=#{address} 12 </sql> 13 14 <!-- 查询所有记录 --> 15 <select id="listAll" resultType="aliasesUser"> 16 select <include refid="cols"/> from user_c 17 </select> 18 19 <!-- 按条件查询 --> 20 <select id="find" parameterType="aliasesUser" resultType="aliasesUser"> 21 select * from user_c 22 <where> 23 <if test="name!=null"> 24 and name like "%"#{name}"%" 25 </if> 26 <if test="age!=null"> 27 and age=#{age} 28 </if> 29 </where> 30 </select> 31 32 <!-- 查询一个用户 --> 33 <select id="get" parameterType="string" resultType="aliasesUser"> 34 select <include refid="cols"/> from user_c where id=#{id} 35 </select> 36 37 <!-- 新增 --> 38 <insert id="create" parameterType="aliasesUser"> 39 insert into user_c (id,name,age,address) values(#{id},#{name},#{age},#{address}) 40 </insert> 41 42 <!-- 修改 --> 43 <update id="update" parameterType="aliasesUser"> 44 update user_c 45 <set> 46 <if test="name!=null"> 47 name=#{name}, 48 </if> 49 <if test="age!=null"> 50 age=#{age}, 51 </if> 52 <if test="address!=null"> 53 address=#{address}, 54 </if> 55 </set> 56 57 where id=#{id} 58 </update> 59 60 <!-- 删除 --> 61 <delete id="delete" parameterType="string"> 62 delete from user_c where id=#{id} 63 </delete> 64 </mapper>
IUserDao.java
1 package cn.itcast.mybatis.dao; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.domain.User; 6 7 8 public interface IUserDao { 9 public List<User> list(); 10 public User get(String id); 11 public int insert(User u); 12 public int update(User u); 13 public int deleteById(String id); 14 }
UserDaoImpl.java
1 package cn.itcast.mybatis.dao; 2 3 import java.util.List; 4 5 import org.mybatis.spring.support.SqlSessionDaoSupport; 6 7 import cn.itcast.mybatis.domain.User; 8 9 public class UserDaoImpl extends SqlSessionDaoSupport implements IUserDao { 10 11 //删除 12 public int deleteById(String id) { 13 return this.getSqlSession().delete("cn.itcast.mybatis.domain.User.deleteOne",id); 14 } 15 16 //新增 17 public int insert(User u) { 18 return this.getSqlSession().insert("cn.itcast.mybatis.domain.User.create", u); 19 } 20 21 //列表 22 public List<User> list() { 23 return this.getSqlSession().selectList("cn.itcast.mybatis.domain.User.listAll"); 24 } 25 26 //修改 27 public int update(User u) { 28 return this.getSqlSession().update("cn.itcast.mybatis.domain.User.update",u); 29 } 30 31 //获取对象 32 public User get(String id) { 33 return (User) this.getSqlSession().selectOne("cn.itcast.mybatis.domain.User.get", id); 34 } 35 36 }
UserServiceDao.java
1 package cn.itcast.mybatis.service; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.domain.User; 6 7 8 public interface IUserService { 9 public List<User> list(); 10 public User get(String id); 11 public int insert(User u); 12 public int update(User u); 13 public int deleteById(String id); 14 }
UserServiceDaoImpl.java
1 package cn.itcast.mybatis.service; 2 3 import java.util.List; 4 5 import cn.itcast.mybatis.dao.IUserDao; 6 import cn.itcast.mybatis.domain.User; 7 8 public class UserServiceImpl implements IUserService { 9 10 private IUserDao userDao; 11 public void setUserDao(IUserDao userDao) { 12 this.userDao = userDao; 13 } 14 15 public int deleteById(String id) { 16 int i = userDao.deleteById(id); 17 //测试事务 18 int j = 1/0; 19 return i; 20 } 21 22 public User get(String id) { 23 return userDao.get(id); 24 } 25 26 public int insert(User u) { 27 return userDao.insert(u); 28 } 29 30 public List<User> list() { 31 return userDao.list(); 32 } 33 34 public int update(User u) { 35 return userDao.update(u); 36 } 37 38 } 39
重头
sqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <typeAliases> 8 <typeAlias type="cn.itcast.mybatis.domain.User" alias="aliasesUser"></typeAlias> 9 </typeAliases> 10 <environments default="development"> 11 12 <environment id="development"> 13 <transactionManager type="JDBC"/> 14 <dataSource type="POOLED"> 15 <property name="driver" value="com.mysql.jdbc.Driver"/> 16 <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> 17 <property name="username" value="root"/> 18 <property name="password" value="friends"/> 19 </dataSource> 20 </environment> 21 <!-- 产品 可用于多个配置的 --> 22 <environment id="product"> 23 <transactionManager type="JDBC"/> 24 <dataSource type="POOLED"> 25 <property name="driver" value="com.mysql.jdbc.Driver"/> 26 <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> 27 <property name="username" value="root"/> 28 <property name="password" value="friends"/> 29 </dataSource> 30 </environment> 31 </environments> 32 <mappers> 33 <mapper resource="cn/itcast/mybatis/domain/User.xml"/> 34 </mappers> 35 </configuration>
spring-beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 15 16 <!-- 数据源 --> 17 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 18 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 19 <property name="url" value="jdbc:mysql://localhost/mybatisdb?characterEncoding=utf-8"></property> 20 <property name="username" value="root"></property> 21 <property name="password" value="friends"></property> 22 </bean> 23 24 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 25 <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> 26 <property name="dataSource" ref="dataSource"></property> 27 28 </bean> 29 30 <!-- 事务相关控制 --> 31 <bean name="transactionManager" 32 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 33 <property name="dataSource" ref="dataSource"/> 34 </bean> 35 36 <!-- 通知 --> 37 <tx:advice id="tx" 38 transaction-manager="transactionManager"> 39 <tx:attributes> 40 <tx:method name="delete*" propagation="REQUIRED" /> 41 <tx:method name="insert*" propagation="REQUIRED" /> 42 <tx:method name="update*" propagation="REQUIRED" /> 43 <tx:method name="find*" read-only="true" /> 44 <tx:method name="get*" read-only="true" /> 45 <tx:method name="select*" read-only="true" /> 46 </tx:attributes> 47 </tx:advice> 48 49 <aop:config> 50 <aop:pointcut id="pc" expression="execution(* cn.itcast.mybatis.service.*.*(..))" /> 51 <!--把事务控制在Service层--> 52 <aop:advisor pointcut-ref="pc" advice-ref="tx" /> 53 </aop:config> 54 55 <bean id="userDao" class="cn.itcast.mybatis.dao.UserDaoImpl"> 56 <property name="sqlSessionFactory" ref="sessionFactory"></property> 57 </bean> 58 59 <bean id="userService" class="cn.itcast.mybatis.service.UserServiceImpl"> 60 <property name="userDao" ref="userDao"></property> 61 </bean> 62 </beans>
测试
1 package test; 2 3 import java.util.List; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 import cn.itcast.mybatis.domain.User; 10 import cn.itcast.mybatis.service.IUserService; 11 12 public class TestSpringMybatis { 13 14 @Test 15 public void test(){ 16 ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); 17 IUserService us = (IUserService) ac.getBean("userService"); 18 List<User> users = us.list(); 19 System.out.println(users.size()); 20 } 21 22 @Test 23 public void testDelete(){ 24 ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); 25 IUserService us = (IUserService) ac.getBean("userService"); 26 us.deleteById("1"); 27 } 28 29 }