spring-mybatis集成的xml配置详解,

    jar包引入

   在web-inf下的lib中,要放入spring必备的五个核心jar包,分别是:

beans,core,context,expresstion.context-support,  依赖jar包logging,日志jar包log4j,..aop切面jar包,mybatis,spring-mybatis,,,connection,jdbc,tx

1.首先,配置数据源文件db.properties..在文件中写入要链接的数据库各项参数,比如链接mysql

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/mybatis
db.username=root
db.password=ou134568

2.创建spring.xml文件,引入beans,context,tx的命名空间

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">

2.加载外部资源,即db.properties

<context:property-placeholder location="classpath:/bms.properties"/>

     3.定义一个数据源,其中${name} 中的值是从外部资源获取的...name必须和资源文件的属性名称相同...且不要使用username属性名称,以免和计算机管理员名称混淆

<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>

</bean>

4.配置一个sqlsessionfactorybean,

注入configLocation对象,值为mybatis.xml文件路径..

注入mapperLocations,值为mappers的资源路径

注入dataSource,值为数据源的id或别名

注入transactionFactory,这是一个事务管理对象.如果没有注入则需要手动提交事务

<bean id="sqlsessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis.xml"/>
<property name="mapperLocations" value="mappers/*.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="transactionFactory">
<bean  class="org.mybatis.spring.transaction.SpringManagedTransactionFactory"></bean>
</property>
</bean>

4.生成dao-mapper

注入basePackage,值为dao的包路径,会把包路径下所有的接口扫描为bean

注入sqlSessionFactoryBeanName  值为上面已经配置的sqlsessionFactoryBean

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"></property>
<property name="sqlSessionFactoryBeanName" >
<idref bean="sqlsessionFactoryBean"/>
</property>
</bean>

5.扫描所有的service实现类,把包路径下所有的类注册

<context:component-scan base-package="service.impl"></context:component-scan>

6.还可以配置一个事务管理,方便通过注解控制事务提交

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="dataSource"></property>
</bean>

7.通过注解来控制事务提交

<tx:annotation-driven transaction-manager="transactionManager"/>

数据库执行dao层只需要提供接口即可,无需实现类...实现类通过sqlsessionfactorybean来获取代理实现类.

业务逻辑层service在实现类中,设置一个私有的dao对象,通过注解来自动注入dao的代理实现类

@Autowired

private UserDAO userDao;


service实现类中查询的实例

@Override
public UserEntity queryUser(String userId) throws Exception {

UserEntity user=userDao.select("userId", userId);

return user;
}

 测试类

package test;


import java.util.ArrayList;
import java.util.List;


import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import dao.UserDAO;
import entity.UserEntity;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.SqlSessionUtil;


public class UserServiceTest {
private UserService us;
private static ApplicationContext ac;
@BeforeClass 
public static void init(){
ac = new ClassPathXmlApplicationContext("classpath:/spring.xml");

}

@Before
public void before(){
us = ac.getBean(UserService.class);
}
@Test
public void test()throws Exception {
List<UserEntity> list= new ArrayList<UserEntity>();
list = us.queryUsers();
System.out.println(list.get(0));

}


}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值