其实网上已经有不少文章描述sm整合,但个人感觉,它们或多或少有些细节没有交代清楚吧,本篇文章就是针对这些细节来进行详解~
首先是项目结构图:
WebContent目录结构图:
User类:
UserMapper以及映射文件:
其中id需要与UserMapper的方法名一致,返回类型resultType以及参数类型parameterType无需多说,需要注意的是findUsers方法,在映射文件中声明了返回
类型是User,而UserMapper返回的是list<User>,实际上这是支持的。
接口IUserService以及它的实现类:
package com.lee.sm.serviceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lee.sm.mapper.UserMapper;
import com.lee.sm.pojo.User;
//默认将类中的所有函数纳入事务管理.
//@Transactional("user")
@Service("iUserServiceImpl")
@Transactional
public class IUserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
/**
* 检查用户登录密码
* */
@Override
public boolean authUser(User user) {
String passWord = userMapper.showPassword(user);
if(passWord != null && user.getPassword() != null && passWord.equals(user.getPassword())){
return true;
}
return false;
}
@Override
public String showUserList() {
List<User> users = userMapper.findUsers();
for(User user : users){
System.out.println(user);
}
return "测试完毕";
}
@Override
public String showStory(User user) {
String userStory = "";
userStory = userStory + "address=[" + userMapper.showAddress(user) + "]";
return userStory;
}
@Override
public String createUser(User user) throws RuntimeException{
userMapper.insertUser(user);
System.out.println("准备抛出异常");
//在插入成功后,抛出一个异常,测试事物是否回滚
try {
int i = 1/0;
} catch (Exception e) {
throw new RuntimeException();
}
return null;
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
原本不是@Service("iUserServiceImpl")而是@Service这样注解的,结果在创建LoginController时报错说,自动注入IUserService时找不到这个bean,
话说@Service不是自动注册为首字母小写的bean吗?....求大神指点......
页面跳转的控制器LoginController:
package com.lee.sm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lee.sm.pojo.User;
import com.lee.sm.serviceImpl.IUserService;
@Controller
@RequestMapping("/mine")
public class LoginController {
@Autowired
@Qualifier("iUserServiceImpl")
private IUserService iUserService;
@RequestMapping
public String mine(){
System.out.println("in the mine()...");
User user = new User();
user.setName("003");
String mineStory = iUserService.showStory(user);
System.out.println(mineStory);
return "main2";
}
@RequestMapping(value="/findFriends")
public String findFriends() {
System.out.println("in the findFriends()");
System.out.println(iUserService.showUserList());
User user = new User("阿诺", "40", "美国加州", 1, "123");
iUserService.createUser(user);
System.out.println("已插入新建用户");
return "main2";
}
public IUserService getiUserService() {
return iUserService;
}
public void setiUserService(IUserService iUserService) {
this.iUserService = iUserService;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MySpringMVCMybatis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring提供转码 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>woder</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>woder</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml:
<?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:cxt="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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 注释此项,因为后面已使用cxt:component-scan --> <!-- <cxt:annotation-config /> --> <cxt:component-scan base-package="com.lee.sm.serviceImpl"> <!-- <cxt:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> </cxt:component-scan> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/jdbc.properties</value> </list> </property> </bean> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="${jdbc.maxActive}"/> <property name="maxIdle" value="${jdbc.maxIdle}"/> <property name="maxWait" value="${jdbc.maxWait}"/> <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/> </bean> <!-- define the SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml"/> </bean> <!-- 开启事务注解驱动 --> <tx:annotation-driven /> <!-- Transaction Manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myDataSource" /> <!-- 如果是user表,用@Transactional("user"),如果是smis用 @Transactional("smis")管理事务--> </bean> <!-- scan for mappers and let them be autowired --> <!-- 使用自动扫描包的方式来注册各种mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lee.sm.mapper" /> </bean> </beans>
woder-servlet.xml,相当于另一些文章说的sprigmvc-servlet.xml:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:cxt="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<cxt:component-scan base-package="com.lee.sm.controller">
<!-- <cxt:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> -->
</cxt:component-scan>
<!-- 主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven/>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="cache" value="true"/> -->
<!-- viewClass属性可以用来指定前台在解析数据时,所允许采用的手段。实际上其默认值就是JstlView -->
<!-- 将来有需要的话,就可以在这里把JstlView改成其它的,如FreeMarkerView,VelocityView,TilesView -->
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
<!-- <mvc:view-controller path="/" view-name="redirect:view/main.jsp"/> -->
<mvc:view-controller path="/" view-name="main"/>
<!-- <mvc:view-controller path="*.jsp"/>-->
</beans>
这里涉及到了mybatis事务无法回滚的问题
原本我是在woder-servlet.xml中使用<cxt:component-scan base-package="com.lee.sm.**">来加载位于该目录下(包括子目录下的所有)所有标注了注解的类的(比如@Controller/@Service),结果mybatis无法回滚
在试过了把@Transactional改成@Transactional(rollbackFor=Exception.class)、查看mysql引擎是否为innodb之后,仍然无效。
最后是把service和controller分别在applicationContext.xml及woder-servlet.xml中加载,才得以解决——这里不得不吐槽某篇文章,因为实际上applicationContext.xml是先于springmvc-servlet.xml加载的,所以service应该在applicationContext.xml中加载,然后springmvc-servlet.xml再加载controller——
那篇文章所叙述的刚好相反...我是在好几次构建controller报错找不到对应的bean注入(service这个bean还没创建)我才醒悟的TnT...
mybatis-config.xml:
<?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>
<!-- <package name="com.chenliang.mvc.entity"/> -->
<typeAlias alias="BaseMapper" type="com.lee.sm.baseMapper.BaseMapper"/>
<typeAlias alias="UserMapper" type="com.lee.sm.mapper.UserMapper"/>
<typeAlias type="com.lee.sm.pojo.User" alias="User"/>
</typeAliases>
<mappers>
<!-- <mapper class="com.lee.sm.mapper.*"/> -->
<mapper resource="com/lee/sm/mapper/UserMapper.xml"/>
</mappers>
</configuration>
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/SPRINGMVC?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=root
jdbc.maxActive=100
jdbc.maxIdle=30
jdbc.maxWait=500
jdbc.defaultAutoCommit=true
这个项目使用的jar包有
mybatis-3.0.6.jar
mybatis-spring-1.0.1-sources.jar
spring-core-3.0.5.RELEASE.jar
spring-webmvc-3.1.1.RELEASE.jar