1、注冊Spring配置文件,在web應用啟動時創建Spring容器(注冊listener)。
<!-- 注冊spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!--注冊contextLoaderListener,在web應用啟動時創建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、字符集過濾器
<!--字符集過濾器,-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、注冊SpringMVC中央調度器
<!--MVC中央調度器,注冊mvc配置文件-->
<servlet>
<servlet-name>SpringMVCDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4、定義實體類,實現頁面。
例如Student類,注冊頁面,歡迎頁面
5、實現處理器,編寫主業務邏輯,等待service注入。
public class RegisterHandler implements Controller {
private IStudentService studentService;//等待注入,設置setter
public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
Integer age = Integer.valueOf(ageStr);
Student student = new Student(name, age);
studentService.registerStudent(student);
ModelAndView modelAndView = new ModelAndView("/welcome.jsp");
modelAndView.addObject("student",student);
return modelAndView;
}
}
6、編寫service層接口,等待dao層注入
public interface IStudentService {
void registerStudent(Student student);
}
public class StudentServiceImpl implements IStudentService {
private IStudentDao studentDao;//等待注入,設置setter
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void registerStudent(Student student) {
studentDao.insertStudent(student);
}
}
7、編寫dao層接口,實現dao層(mapper映射)。最好把mapper映射放在dao包下,但是這樣有個問題就是不會被編譯。嘗試放在resources文件夾下
public interface IStudentDao {
void insertStudent(Student student);
}
//IStudentDao.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="dao.IStudentDao">
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
insert into student(name,age) values(#{name},#{age})
</insert>
</mapper>
8、數據庫連接,數據源
//jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai
jdbc.user=1234
jdbc.password=1234
//spring-db.xml
<!-- c3p0 數據源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--注冊數據庫配置屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
9、整合spring與mybatis
重點:sqlSessionFactoryBean、mapper掃描器
<!--注冊SqlSessionFactory的Bean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--mapper掃描器,指定掃描mapper映射文件的位置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="basePackage" value="dao"/>
</bean>
10、dao注入到service,service注入到handler
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studentDao" ref="IStudentDao"/>
</bean>
<bean id="/test/register.do" class="handlers.RegisterHandler">
<property name="studentService" ref="studentService"/>
</bean>
11、最后在pom里編譯一下沒放進resources目錄的xml文件
<build>
<!--重要!!!-->
<!-- 編譯java目錄下的xml文件(不在resources目錄下的) -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
12、添加事務處理
<!-- 注冊事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="register*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- AspectJ的aop配置-->
<aop:config>
<aop:pointcut id="point" expression="execution(* service.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="point"/>
</aop:config>