Spring整合MyBatis框架
- 目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中;整合原理就是把Dao生成的代理对象,存入到IOC容器中
- 在service中注入dao对象,进行测试
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载数据库配置属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--开启注解扫描,要扫描的是service-->
<context:component-scan base-package="com.qcby.service"/>
<!--配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--Spring整合MyBatis框架,SqlSessionFactoryBean创建工厂对象-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.qcby.entity" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置扫描Dao接口包,动态实现Dao接口,注入到ioc容器中-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qcby.dao"/>
</bean>
</beans>
service代码如下
package com.qcby.service;
import com.qcby.dao.AccountDao;
import com.qcby.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
//查询所有
@Override
public List<Account> findAll() {
System.out.println("业务层:查询所有");
List<Account> list = accountDao.findAll();
return list;
}
}
controller代码如下
package com.qcby.controller;
import com.qcby.entity.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
//依赖注入
@Autowired
private AccountService accountService;
/**
* 查询所有
* @return
*/
@RequestMapping("/findAll")
public ModelAndView findAll(){
System.out.println("表现层:查询所有");
//调用service的方法
List<Account> list = accountService.findAll();
for (Account account : list) {
System.out.println(account);
}
ModelAndView mv = new ModelAndView();
mv.setViewName("suc");
return mv;
}
}
配置声明式事务管理
<!-- 配置声明式事务管理-->
<!--平台事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!--配置事务的增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.qcby.service.*ServiceImpl.*(..))"/>
</aop:config>
表单代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/account/findAll">查询所有</a>
<form action="/account/save" method="post">
姓名:<input type="text" name="name" /><br/>
金额:<input type="text" name="money" /><br/>
<input type="submit" value="保存" />
</form>
</body>
</html>
controller代码
package com.qcby.controller;
import com.qcby.entity.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
//依赖注入
@Autowired
private AccountService accountService;
/**
* 查询所有
* @return
*/
@RequestMapping("/findAll")
public ModelAndView findAll(){
System.out.println("表现层:查询所有");
//调用service的方法
List<Account> list = accountService.findAll();
for (Account account : list) {
System.out.println(account);
}
ModelAndView mv = new ModelAndView();
mv.setViewName("suc");
return mv;
}
@RequestMapping("/save")
public String save(Account account){
//Service的保存方法
accountService.save(account);
return "suc";
}
}
service接口和实现类代码
package com.qcby.service;
import com.qcby.entity.Account;
import java.util.List;
public interface AccountService {
//查询所有
public List<Account> findAll();
//保存
void save(Account account);
}
package com.qcby.service;
import com.qcby.dao.AccountDao;
import com.qcby.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
//查询所有
@Override
public List<Account> findAll() {
System.out.println("业务层:查询所有");
List<Account> list = accountDao.findAll();
return list;
}
@Override
public void save(Account account) {
accountDao.save(account);
}
}
dao代码
package com.qcby.dao;
import com.qcby.entity.Account;
import java.util.List;
public interface AccountDao {
//查询所有
public List<Account> findAll();
//保存
void save(Account account);
}
dao.xml代码
<?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">
<mapper namespace="com.qcby.dao.AccountDao">
<select id="findAll" resultType="account">
select * from account
</select>
<insert id="save" parameterType="account">
insert into account (name,money) values(#{name},#{money})
</insert>
</mapper>