39--SSM整合(方案二)
项目环境搭建(SSM整合)
项目环境搭建
1、创建Maven的简单web工程
2、补全WEB-INF目录和web.xml文件
3、创建包和目录
4、在pom.xml文件中,引入junit、log4j、servlet等依赖包
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 整合log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<!-- Servlet/JSP/JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
5、在resources目录下创建log4j.properties文件,配置内容如下:
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %m%n
6、在resources目录下创建jdbc.properties文件,配置内容如下:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///yonghedb?characterEncoding=utf-8
db.username=root
db.password=root
整合spring框架
1、在pom.xml文件中,引入spring的依赖包
<!-- 整合spring框架(包含springmvc)
这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
2、在src/main/resources/spring目录下,创建spring的核心配置文件:applicationContext.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">
</beans>
整合springmvc框架
前面在添加spring的jar包同时,将springmvc的jar包也引入了,因此这里不再引入springmvc的jar包
1、在resources/spring目录下,创建springmvc的核心配置文件:springmvc-config.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">
<!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) -->
<mvc:default-servlet-handler/>
<!-- 2.配置注解驱动,用于识别注解(比如@Controller) -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类,
如果扫描到的类上有 @Controller、@Service、@Component等注解,
将会自动将类注册为bean
-->
<context:component-scan base-package="com.tedu.controller">
</context:component-scan>
<!-- 4.配置内部资源视图解析器
prefix:配置路径前缀
suffix:配置文件后缀
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2、在web.xml中配置springmvc
<!-- 配置springmvc, 将所有请求交给springmvc来处理 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc核心配置文件的位置,默认Springmvc的配置文件是在WEB-INF目录下,默认的名字为springmvc-servlet.xml,如果要放在其他目录,则需要指定如下配置:
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</init-param>
</servlet>
<!-- 其中的斜杠(/)表示拦截所有请求(除JSP以外), 所有请求都要经过springmvc前端控制器 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3、配置springmvc乱码处理过滤器
<!-- 乱码处理过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<!-- 指定拦截方式为拦截所有请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
3、测试springmvc框架:
(1)创建test.jsp页面
在WEB-INF/pages/目录下,创建test.jsp页面。
<%@ page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1>yonghe-ssm test.jsp~~~</h1>
</body>
</html>
(2)创建测试Controller: DoorController
/** 测试类:测试springmvc开发环境 */
@Controller
public class DoorController {
@RequestMapping("/testmvc")
public String testmvc(){
return "test";
}
}
(3)将项目部署到服务器中,启动服务器:
访问效果如下:
整合mybatis框架
1、在pom.xml文件中,引入mybatis及相关依赖包
<!-- 整合mybatis框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
2、在resources/mybatis目录下,创建mybatis的核心配置文件: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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置开发环境 -->
<environments default="develop">
<!-- 这里可以配置多个环境,比如develop,test等 -->
<environment id="develop">
<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
<transactionManager type="JDBC"></transactionManager>
<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yonghedb?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
<mappers>
<mapper resource="mybatis/mapper/DoorMapper.xml"/>
</mappers>
</configuration>
3、在src/main/resources/mybatis/mapper目录下,创建Door实体类的映射文件:DoorMapper.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">
<!-- 门店表的映射文件 namespace值为对应接口的全路径 -->
<mapper namespace="com.tedu.dao.DoorMapper">
<!-- 1.查询所有门店信息,id值为对应接口中方法的名字
resultType指定将查询的结果封装到哪个pojo对象中
-->
<select id="findAll" resultType="com.tedu.pojo.Door">
select * from tb_door
</select>
</mapper>
4、在mybatis的全局配置文件中引入DoorMapper.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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置开发环境 -->
...
<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
<!-- 配置映射文件 -->
<mappers>
<mapper resource="mybatis/mapper/DoorMapper.xml"/>
</mappers>
</configuration>
5、创建com.tedu.dao.DoorMapper接口,并根据EmpMapper.xml文件中的sql语句,提供findAll方法
package com.tedu.dao;
import java.util.List;
import com.tedu.pojo.Door;
/**
* DoorMapper接口
* 声明增删改查方法,对门店信息进行操作
*/
public interface DoorMapper {
/**
* 1.查询所有门店信息
*/
public List<Door> findAll();
}
6、创建实体类Door,用于封装所有的门店信息
package com.tedu.pojo;
public class Door {
private Integer id; //门店编号
private String name; //门店名称
private String tel; //门店电话
private String addr; //门店地址
//getter和setter方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
//重写toString方法
@Override
public String toString() {
return "Door [id=" + id + ", name=" + name + ", tel=" + tel + ", addr=" + addr + "]";
}
}
7、创建测试com.tedu.controller.TestMybatis类,对mybatis开发环境进行测试
package com.tedu.controller;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.tedu.dao.DoorMapper;
import com.tedu.pojo.Door;
/** 测试类:测试mybatis开发环境 */
public class TestMybatis {
public static void main(String[] args) throws Exception {
//1.读取mybatis-config.xml核心文件
InputStream in = Resources.getResourceAsStream(
"mybatis/mybatis-config.xml");
//2.获取SqlSessionFactory工厂
SqlSessionFactory factory =
new SqlSessionFactoryBuilder()
.build(in);
//3.获取SqlSession对象
SqlSession session = factory.openSession();
//4.获取DoorMapper接口的实例
DoorMapper mapper = session.getMapper(DoorMapper.class);
//5.调用findAll方法查询所有门店信息
List<Door> list = mapper.findAll();
//6.遍历所有门店信息
for(Door door : list){
System.out.println(door);
}
}
}
执行结果:
整合spring和mybatis
1、修改mybatis-config.xml文件,将数据源等配置移除,在spring的配置文件中配置
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置开发环境 -->
<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
</configuration>
2、在applicationContext.xml中配置 druid连接池、SqlSession工厂等
<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">
<!-- 1.加载jdbc.properties文件的位置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置连接数据库的基本信息 -->
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<!-- 3.整合spring和mybatis框架
将SqlSession等对象的创建交给Spring容器
id值(sqlSessionFactory)是固定值
-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 3.1.指定mybatis核心配置文件的位置 -->
<property name="configLocation"
value="classpath:mybatis/mybatis-config.xml"></property>
<!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!-- 4、定义mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 -->
<property name="basePackage"
value="com.tedu.dao"/>
</bean>
</beans>
3、在resources目录下创建jdbc.properties文件,将连接数据库的基本信息提取到文件中
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///yonghedb?characterEncoding=utf-8
db.username=root
db.password=root
4、复制TestController类,改名为TestSSM
package com.tedu.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tedu.dao.DoorMapper;
import com.tedu.pojo.Door;
/** 测试类:测试SSM开发环境 */
@Controller /* 这个注解表示当前类属于Controller层代码 */
public class TestSSM {
/** 自动装配:由spring自动为属性赋值(对象) */
@Autowired
DoorMapper mapper;
@RequestMapping("/testssm")
public String testSSM(){
//1.调用findAll方法查询所有门店信息
List<Door> list = mapper.findAll();
//2.遍历所有门店信息
for(Door door : list){
System.out.println(door);
}
return "test";
}
}