1、集成思路
-
使用idea新建普通的javaWeb项目
-
导入jar包
-
配置spring的核心配置文件 : 引入jdbc.properties 配置dataSource
配置SqlSessionFactory domain、dao(mapper)service 事物tx controller -
springMvc核心配置文件: 扫描包 静态资源放行 开启注解支持 配置视图解析器
-
web.xml配置: 启动spring容器 启动springMvc容器 解决post提交乱码问题
-
jsp完成
2.导入jar包
3.1 配置spring的核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
">
<!--加载jdbc.properties文件-->
<!--
system-properties-mode="NEVER"
设置这个是为了如果jdbc.properties没有写前缀,那么系统会默认用户名
-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 扫描service层-->
<context:component-scan base-package="cn.itsource.ssm.service"/>
<!--配置连接池对象-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
</bean>
<!--创建SessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 去加载所有的mapper.xml文件-->
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*.xml"/>
<!--定义公共的基础包-->
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.query
cn.itsource.ssm.domain
</value>
</property>
</bean>
<!-- dao配置完毕 方式1-->
<!--方式1:如果有很多个mapper,需要每个都配置。比较麻烦,使用另一种方式-->
<bean id="employeeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="cn.itsource.ssm.mapper.EmployeeMapper"/>
</bean>
<!--方式2-->
<!--mapper扫描器的配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--只要扫描到该包下所有的接口,我都使用代理模式进行实现-->
<property name="basePackage" value="cn.itsource.ssm.mapper"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解的支持-->
<tx:annotation-driven/>
</beans>
3.2 jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/ssm
jdbc.username=root
jdbc.password=123456
3.3 log4j.properties
###全局 配置根
##log4j常见的日志等级: trace<debug<info<warn<error
log4j.rootLogger = ERROR,console
##输出局部的日志信息 打印的日志等级要大于或者等于trace等级
log4j.logger.cn.itsource=trace
##打印的日志规则 日志信息打印在控制台里面
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
##你打印的日志是有一定格式的
log4j.appender.console.layout = org.apache.log4j.PatternLayout
##讲解详细布局规则
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
3.4 domain
package cn.itsource.ssm.domain;
public class Employee {
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.5 dao(mapper)
package cn.itsource.ssm.mapper;
import cn.itsource.ssm.domain.Employee;
import java.util.List;
public interface EmployeeMapper {
void save(Employee employee);
List<Employee> selectAll();
}
3.6 service
package cn.itsource.ssm.service;
import cn.itsource.ssm.domain.Employee;
import java.util.List;
public interface IEmployeeService {
void save(Employee employee);
void update(Employee employee);
void delete(Long id);
Employee selectById(Long id);
List<Employee> selectAll();
}
3.7 servicImpl
package cn.itsource.ssm.service.impl;
import cn.itsource.ssm.domain.Employee;
import cn.itsource.ssm.mapper.EmployeeMapper;
import cn.itsource.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
@Transactional
public void save(Employee employee) {
employeeMapper.save(employee);
System.out.println(1/0);
}
@Override
@Transactional
public void update(Employee employee) {
}
@Override
@Transactional
public void delete(Long id) {
}
@Override
public Employee selectById(Long id) {
return null;
}
@Override
public List<Employee> selectAll() {
return employeeMapper.selectAll();
}
}
3.8 controller
package cn.itsource.ssm.web.controller;
import cn.itsource.ssm.service.IEmployeeService;
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("/employee")
public class EmployeeController {
@Autowired
private IEmployeeService employeeService;
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("emps", employeeService.selectAll());
return "employee/employee";
}
}
4.applicationContext-mvc.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
">
<!-- 扫描service层-->
<context:component-scan base-package="cn.itsource.ssm.web.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/views/"/>
</bean>
</beans>
5.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_4_0.xsd"
version="4.0">
<!--在指定的位置加装applicationContext.xml文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<!--启动springMvc容器-->
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<!--解决post提交乱码问题-->
<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>
</web-app>