Mybatis和springMVC整合
一、对于一些初步学习框架的同学而言,配置环境无疑是让人头疼的事情,感觉条理很混乱,接下来我这篇文章就是讲解一下mybatis和springmvc的一些整合步骤,屡清一下思路。
二、开发工具:IntelliJ IDEA
数据库:Mysql
开发环境:jdk1.8 tomcat8
三、导入jar包,
四、在src目录下新建config目录,用来存放一些配置文件,使项目更加条理清晰,然后分别新建mybatis和spring目录,再建一个com包,存放java代码。如图所示,
db.properties是建立在config目录下的,里面配置的是数据库连接文件,内容如下
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=123456
当然这是我的数据库连接信息,你要改成自己的。
在mybatis目录下新建SqlMaoConfig.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>
<!--UserMapper.XML
参数的根路径 -->
<typeAliases>
<package name="com_sweep.pojo"></package>
</typeAliases>
<mappers>
<mapper resource="com_sweep/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
配置dao层文件,在spring包下新建Applicationcontext-dao.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd ">
<!--
加载配置文件 -->
<context:property-placeholder
location="classpath:config/db.properties"
/>
<!--
数据库连接池 -->
<bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${db.driver}"
/>
<property name="url"
value="${db.url}"
/>
<property name="username"
value="${db.username}"
/>
<property name="password"
value="${db.password}"
/>
<property name="maxActive"
value="10"
/>
<property name="maxIdle"
value="5"
/>
</bean>
<!-- mapper配置
-->
<!--
让spring管理sqlsessionfactory
使用mybatis和spring整合包中的
-->
<bean
id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--
数据库连接池 -->
<property
name="dataSource"
ref="dataSource"
/>
<!--
加载mybatis的全局配置文件
-->
<property
name="configLocation"
value="classpath:config/mybatis/SqlMapConfig.xml"
/>
</bean>
<!--
配置Mapper扫描器
-->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com_sweep.mapper"/>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
</bean>
<!--
事务管理器 -->
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--
数据源 -->
<property
name="dataSource"
ref="dataSource"
/>
</bean>
<!--
通知 -->
<tx:advice
id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--
传播行为 -->
<tx:method
name="save*"
propagation="REQUIRED"
/>
<tx:method
name="insert*"
propagation="REQUIRED"
/>
<tx:method
name="delete*"
propagation="REQUIRED"
/>
<tx:method
name="update*"
propagation="REQUIRED"
/>
<tx:method
name="find*"
propagation="SUPPORTS"
read-only="true"
/>
<tx:method
name="get*"
propagation="SUPPORTS"
read-only="true"
/>
<tx:method
name="query*"
propagation="SUPPORTS"
read-only="true"
/>
<tx:method
name="select*"
propagation="SUPPORTS"
read-only="true"
/>
</tx:attributes>
</tx:advice>
<!--
切面 -->
<aop:config>
<aop:advisor
advice-ref="txAdvice"
pointcut="execution(* com_sweep.service.*.*(..))"
/>
</aop:config>
</beans>
在spring目录下新建spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<context:component-scan
base-package="com_sweep"/>
<!--
注解驱动器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--
视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"
value="/WEB-INF/pages/"/>
<property name="suffix"
value=".jsp"/>
</bean>
<!--
静态资源处理 -->
<mvc:default-servlet-handler/>
</beans>
在spring目录下新建Application-service.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- @Service扫描
-->
<context:component-scan
base-package="com_sweep.service"></context:component-scan>
</beans>
Web.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--
加载spring容器
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/Applicationcontext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc前端控制器
-->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-mvc.xml</param-value>
</init-param>
<!--
在tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--
配置Post请求乱码
-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
到现在为止,基本就配置完成了,现在测试一下,在pojo目录下新建User类,在mapper目录下新建UserMapper接口:
package
com_sweep.mapper;
import com_sweep.pojo.User;
public interface UserMapper {
public
User findUserById(int
id);
}
在mapper目录下新建UserMapper.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_sweep.mapper.UserMapper">
<select id="findUserById"
parameterType="int"
resultType="User">
select
* from users where id=#{id}
</select>
</mapper>
Service层定义一个UserService接口,和他的实现类,在controller下新建UserController类如下:
package
com_sweep.controller;
import com_sweep.mapper.UserMapper;
import com_sweep.pojo.User;
import com_sweep.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private
UserService userService;
@RequestMapping("/login")
public
String login(Integer id,Model model){
User user=userService.findUserById(id);
model.addAttribute("u",user);
return "hello";
}
}