Spring与MyBatis整合

此篇和我之前写的spring单元测试,springMVC测试,MyBatis测试由很大关联,可以结合着看

一.包

 

二.整体结构

三.代码

1.spring.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" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7                http://www.springframework.org/schema/context
 8                http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9                http://www.springframework.org/schema/tx
10                http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
11                http://www.springframework.org/schema/aop
12                http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13                ">
14     <!-- 自动扫描dao和service包(自动注入) -->
15     <context:component-scan base-package="com.youms.ssh.webService" />
16 
17     <!-- 配置视图解析器: 负责将视图名解析成真正的视图对象(比如jsp) -->
18     <bean
19         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix" value="/WEB-INF/"></property>
21         <property name="suffix" value=".jsp"></property>
22     </bean>
23     
24     <!-- 加载配置文件的简便方法 -->
25     <context:property-placeholder location="classpath:config.properties"/>
26     <!-- 将多个配置文件读取到容器中,交给Spring管理 -->  
27     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
28         <property name="locations">  
29            <list>  
30               <!-- 这里支持多种寻址方式:classpath和file -->  
31               <!-- <value>classpath:config.properties</value> -->
32               <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
33               <!--<value>file:/opt/demo/config/demo-remote.properties</value> -->  
34             </list>  
35         </property>  
36     </bean>  
37     
38     <!-- 创建DataSource对象 -->
39     <bean id="jdbc" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
40         <property name="driverClassName" value="${jdbc.utf-8.driver}" />
41         <property name="url" value="${jdbc.utf-8.jdbcUrl}" />
42         <property name="username" value="${jdbc.utf-8.username}" />
43         <property name="password" value="${jdbc.utf-8.password}" />
44     </bean>
45     
46     <!-- 创建SqlSessionFactory对象 -->
47     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
48         <!-- 数据库连接参数 -->
49         <property name="dataSource" ref="jdbc"/>
50         <!-- SQL定义文件参数 -->
51         <property name="mapperLocations" value="classpath:mapper/*.xml"/>
52     </bean>
53     
54     <!-- 可以不创建sqlSessionTemplate,运用sqlSessionFactory对象创建session同样可以执行查询语句 -->
55     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
56         <constructor-arg ref="sqlSessionFactory" />
57     </bean>
58     
59     <!-- ==========以下是自动生成mapper接口的实现的配置,如果自己写接口的 实现则不用配置===========-->
60     <!-- 根据Mapper接口生成实现对象,默认接口名首字母小写做id属性 -->
61     <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> -->
62         <!-- 指定扫描Mapper接口的路径 -->
63         <!-- <property name="basePackage" value="com.youms.ssh.webService.dao"/> -->
64         <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"/> -->
65     <!-- </bean> -->
66 </beans>
2.StuScoreImpl
 1 package com.youms.ssh.webService.dao.impl;
 2 
 3 import org.mybatis.spring.SqlSessionTemplate;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.beans.factory.annotation.Qualifier;
 6 import org.springframework.stereotype.Repository;
 7 
 8 import com.youms.ssh.webService.dao.StuScoreDao;
 9 import com.youms.ssh.webService.entity.StuScore;
10 
11 @Repository("stuScoreDao")
12 public class StuScoreImpl implements StuScoreDao {
13     
14     @Autowired
15     //@Qualifier("sqlSessionTemplate")
16     private SqlSessionTemplate sqlSessionTemplate;
17     
18     /**
19      * 根据学号查询学生
20      * @param stuid
21      * @return
22      */
23     @Override
24     public StuScore selectByStuid(Integer stuid) {
25         // TODO Auto-generated method stub
26         return sqlSessionTemplate.selectOne("selectByStuid", stuid);
27     }
28 
29     /**
30      * 增加学生
31      * @param stu
32      */
33     @Override
34     public void insertStu(StuScore stu) {
35         // TODO Auto-generated method stub
36         this.sqlSessionTemplate.insert("insertStu",stu);
37     }
38 
39     /**
40      * 根据stuid删除学生
41      * @param stu
42      */
43     @Override
44     public void deleteStu(Integer stuid) {
45         // TODO Auto-generated method stub
46         this.sqlSessionTemplate.delete("deleteStu", stuid);
47     }
48 
49     /**
50      * 更新学生数据
51      * @param stu
52      */
53     @Override
54     public void updateStu(StuScore stu) {
55         // TODO Auto-generated method stub
56         this.sqlSessionTemplate.update("updateStu", stu);
57     }
58 
59 }
3.MyBatisTest
 1 package test;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 import org.mybatis.spring.SqlSessionTemplate;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 import com.youms.ssh.webService.dao.StuScoreDao;
11 import com.youms.ssh.webService.entity.StuScore;
12 
13 public class MyBatisTest {
14     private StuScoreDao stuScoreDao;
15     private SqlSessionTemplate sqlSessionTemplate;
16 
17     @Before
18     public void setUp() throws Exception {
19         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
20         //sqlSessionTemplate = ac.getBean(SqlSessionTemplate.class);
21         stuScoreDao = (StuScoreDao) ac.getBean("stuScoreDao");
22         System.out.println(stuScoreDao);
23     }
24 
25     @After
26     public void tearDown() throws Exception {
27     }
28 
29     @Test
30     //根据学号查询学生
31     public void test() {
32         ///StuScore stu = sqlSessionTemplate.selectOne("selectByStuid",2);
33         StuScore stu = stuScoreDao.selectByStuid(2);
34         System.out.println(stu.getName());
35     }
36 
37     
38     @Test
39     //增加学生
40     public void test2(){
41         StuScore stuScore = new StuScore();
42         stuScore.setName("测试1");
43         stuScore.setStuid(10);
44         this.stuScoreDao.insertStu(stuScore);
45     }
46     
47     @Test
48     //删除
49     public void test3(){
50         this.stuScoreDao.deleteStu(10);
51     }
52     
53     @Test
54     //修改数据
55     public void test4(){
56         StuScore stu = new StuScore();
57         stu.setName("测试11");
58         stu.setStuid(10);
59         this.stuScoreDao.updateStu(stu);
60     }
61 }

其他代码和我之前写的spring单元测试,springMVC测试,MyBatis测试中的代码是相同的

 

posted @ 2016-10-31 10:58  执迷而不悔  阅读(249)  评论(0编辑  收藏  举报