二话不说,先导入一波依赖
大概的包架构:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringMvc04</groupId>
<artifactId>SpringMvc04</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMvc04 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--MyBatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--MySQL的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
<!--SpringMVC的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring连接数据库的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring配置切面的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring的上下文的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--Spring和MyBatis和整合依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--C3p0的依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!--servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMvc04</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
这里注意一手resources下面的的扫描,不然写在java资源下面的xml文件会扫描不到。
配置mvc,springmvc.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:mvc="http://www.springframework.org/schema/mvc"
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.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"></context:component-scan>
<!--mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
之后便是整合的主要配置文件application-common.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: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.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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--连接池数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
</bean>
<!--sqlsessionfacotory工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.model"></property>
<property name="mapperLocations" value="classpath:com/model/*.xml"></property>
</bean>
<!--接口扫描器-->
<bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"></property>
</bean>
<!--事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--切面-->
<tx:advice transaction-manager="transactionManager" id="advice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="sel*" read-only="true"/>
<tx:method name="*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--切点配置-->
<aop:config>
<aop:pointcut id="putcut" expression="execution(* com.services.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="advice" pointcut-ref="putcut"></aop:advisor>
</aop:config>
</beans>
Mybatis实体映射文件EmpMapper与EmpMapper.XML文件:
EmpMapper:
package com.mapper;
import com.model.Sy_Emp;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("empMapper")
public interface EmpMapper {
public List<Sy_Emp> selAll();
public Sy_Emp selById(int id);
public void addSyEmp(Sy_Emp emp);
public void delSyEmp(int id);
public void updateSyEmp(Sy_Emp emp);
public Sy_Emp loginEmp(Sy_Emp emp);
}
EmpMapper.xml:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.EmpMapper">
<select id="selAll" resultType="Sy_Emp">
select * from sy_emp
</select>
<select id="selById" parameterType="int" resultType="Sy_Emp">
select * from sy_emp where id=#{id}
</select>
<insert id="addSyEmp" parameterType="Sy_Emp" >
insert into sy_emp(empname,empno,pwd,remark)values(#{empname},#{empno},#{pwd},#{remark});
</insert>
<delete id="delSyEmp" parameterType="int" >
delete from sy_emp where id=#{id}
</delete>
<update id="updateSyEmp" parameterType="Sy_Emp">
update sy_emp set empname=#{empname},empno=#{empno},pwd=#{pwd},remark=#{remark} where id=#{id}
</update>
<select id="loginEmp" parameterType="Sy_Emp" resultType="Sy_Emp">
select * from sy_emp where empno=#{empno} and pwd=#{pwd}
</select>
</mapper>
Ok!到这里MyBatis就配置完成
之后是事物层与Controller的对象注入了
TestController:
package com.controller;
import com.model.Sy_Emp;
import com.services.ISyEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@Autowired
private ISyEmpService empService;
@RequestMapping("test")
public String test(){
for (Sy_Emp emp : empService.selAll()) {
System.out.println(emp);
}
return "success";
}
}
EmpServiceImpl:
package com.services;
import com.mapper.EmpMapper;
import com.model.Sy_Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("empService")
public class EmpServiceImpl implements ISyEmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Sy_Emp> selAll() {
return empMapper.selAll();
}
@Override
public Sy_Emp selById(int id) {
return empMapper.selById(id);
}
@Override
public void addSyEmp(Sy_Emp emp) {
empMapper.addSyEmp(emp);
}
@Override
public void delSyEmp(int id) {
empMapper.delSyEmp(id);
}
@Override
public void updateSyEmp(Sy_Emp emp) {
empMapper.updateSyEmp(emp);
}
@Override
public Sy_Emp loginEmp(Sy_Emp emp) {
return empMapper.loginEmp(emp);
}
}
注意注解service的命名与@Autowired
如果不行可以采用手动注入的方式:@Resource(name = “usersService”,type = com.ssm.services.impl.UsersServiceImpl.class)
这样基本的整合就已经完成了,最后就是web.xml的配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--加载整合配置的xml文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationConfig-common.xml</param-value>
</context-param>
<!---spring的核心监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc的servlet配置-->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
这样就Ok了!