文章目录
- 一、环境准备
-
- 1.1、创建数据库和表
- 1.2、导入框架依赖的jar包
- 1.3、修改Maven的编译版本
- 1.4、完善Maven目录
- 1.5、编写项目需要的包
- 1.6、编写实体、Mapper、Service
- 二、配置MyBatis环境
-
- 2.1、配置mybatis的主配置文件
- 2.2、编写映射文件
- 2.3、测试环境是否正确
- 三、配置Spring环境
-
- 3.1、编写主配置文件
- 3.2、测试Spring环境是否正确
- 四、Spring整合MyBaits框架
-
- 4.1、定义数据库配置
- 4.2、主配置文件中引入properties配置文件
- 4.3、注册数据源
- 4.4、定义事务管理器
- 4.5、开启事务的注解驱动支持
- 4.6、配置SqlSessionFactoryBean
- 4.7、配置自动扫描所有 Mapper 接口和文件
- 4.8、测试
- 五、配置SpringMVC环境
-
- 5.1、主配置文件
- 5.2、web.xml配置前端控制器
- 六、Spring集成SpringMVC环境
一、环境准备
1.1、创建数据库和表
create database ssm;
use ssm;
create table t_account(
id int primary key auto_increment,
name varchar(30),
age int,
balance int
);
insert into t_account values(default,'HelloWorld',23,100);
1.2、导入框架依赖的jar包
由于我们整合的是SpringMVC、Spring和MyBatis,所以需要将这三个框架所需要的jar包导入到项目中。注意:由于要连接数据库,所以仍需要数据库驱动包。
注意:MyBatis框架和Spring框架整合需要一个mybatis-spring的jar包,该jar包的作用是两个框架的转换包。
dependencies>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>3.5.3version>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>5.1.6version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.2.1.RELEASEversion>
dependency>
dependency>
groupId>org.aspectjgroupId>
artifactId>aspectjweaverartifactId>
version>1.9.4version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-txartifactId>
version>5.2.1.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
version>5.2.1.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webartifactId>
version>5.2.1.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webmvcartifactId>
version>5.2.1.RELEASEversion>
dependency>
dependency>
groupId>com.fasterxml.jackson.coregroupId>
artifactId>jackson-coreartifactId>
version>2.9.5version>
dependency>
dependency>
groupId>com.fasterxml.jackson.coregroupId>
artifactId>jackson-annotationsartifactId>
version>2.9.5version>
dependency>
dependency>
groupId>com.fasterxml.jackson.coregroupId>
artifactId>jackson-databindartifactId>
version>2.9.5version>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>javax.servlet-apiartifactId>
version>3.1.0version>
scope>providedscope>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>jstlartifactId>
version>1.2version>
dependency>
dependency>
groupId>commons-fileuploadgroupId>
artifactId>commons-fileuploadartifactId>
version>1.3.1version>
dependency>
dependency>
groupId>commons-iogroupId>
artifactId>commons-ioartifactId>
version>2.4version>
dependency>
dependency>
groupId>log4jgroupId>
artifactId>log4jartifactId>
version>1.2.17version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-testartifactId>
version>5.2.1.RELEASEversion>
scope>testscope>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.12version>
scope>testscope>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatis-springartifactId>
version>2.0.3version>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
version>1.1.21version>
dependency>
dependencies>
1.3、修改Maven的编译版本
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
maven.compiler.source>1.8maven.compiler.source>
maven.compiler.target>1.8maven.compiler.target>
properties>
1.4、完善Maven目录
1.5、编写项目需要的包
cn.bdqn.domain
cn.bdqn.mapper
cn.bdqn.service
cn.bdqn.controller
cn.bdqn.utils
cn.bdqn.exception
1.6、编写实体、Mapper、Service
public class Account {
private int id;
private String name;
private Integer age;
private Integer balance;
// 生成get和set方法、toString方法
}
public interface AccountMapper {
// 查询所有的账号
public ListAccount> selectAll();
// 保存账号
public void insert(Account account);
}
public interface AccountService {
// 查询所有账号
public ListAccount> queryAll();
// 保存账号
public void save(Account account);
}
public class AccountServiceImpl implements AccountService {
// 查询全部账号
public ListAccount> queryAll() {
System.out.println("查询全部账号");
return null;
}
// 保存账号
public void save(Account account) {
System.out.println("保存账号");
}
}
二、配置MyBatis环境
2.1、配置mybatis的主配置文件
主配置文件:mybaits-config.xml.
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
configuration>
typeAliases>
package name="cn.bdqn.domain"/>
typeAliases>
environments default="mysql">
environment id="mysql">
transactionManager type="JDBC">transactionManager>
dataSource type="POOLED">
property name="driver" value="com.mysql.jdbc.Driver"/>
property name="url" value="jdbc:mysql:///ssm?allowMultiQueries=true"/>
property name="username" value="root"/>
property name="password" value="1234567"/>
dataSource>
environment>
environments>
mappers>
package name="cn.bdqn.mapper"/>
mappers>
configuration>
2.2、编写映射文件
在resources目录下新建与mapper接口同名的目录。即:cn/bdqn/mapper
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
mapper namespace="cn.bdqn.mapper.AccountMapper">
select id="selectAll" resultType="cn.bdqn.domain.Account">
select
id,name,age,balance
from
t_account
select>
insert id="insert">
insert into t_account(name,age,balance) values (#{name},#{age},#{balance})
insert>
mapper>
2.3、测试环境是否正确
@Test
public void testMyBatis() throws Exception{
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession session = ssf.openSession();
AccountMapper accountMapper = session.getMapper(AccountMapper.class);
ListAccount> accounts = accountMapper.selectAll();
System.out.println(accounts);
is.close();
session.close();
}
三、配置Spring环境
3.1、编写主配置文件
注意:由于是SSM项目整合,在Spring主配置文件中配置扫描包的时候仅仅针对Service业务层、Mapper持久化层等注解的扫描,对于Web层,交由SpringMVC框架去处理。
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
context:component-scan base-package="cn.bdqn">
context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
beans>
3.2、测试Spring环境是否正确
@Test
public void testSpring() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) ac.getBean("accountService");
System.out.println(accountService);
}
四、Spring整合MyBaits框架
注意:我们在配置MyBaits环境的时候,数据源配置在了MyBatis上,现在由于Spring整合MyBaits,则对于在MyBatis中配置的数据源要交给Spring去管理。在这里,我们用的阿里巴巴的数据源。
官网
4.1、定义数据库配置
定义db.properties数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm?allowMultiQueries=true
jdbc.username=root
jdbc.password=1234567
4.2、主配置文件中引入properties配置文件
context:property-placeholder location="classpath:db.properties"/>
4.3、注册数据源
bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
property name="driverClassName" value="${jdbc.driver}"/>
property name="url" value="${jdbc.url}"/>
property name="username" value="${jdbc.username}"/>
property name="password" value="${jdbc.password}"/>
bean>
4.4、定义事务管理器
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource"/>
bean>
4.5、开启事务的注解驱动支持
tx:annotation-driven transaction-manager="transactionManager"/>
4.6、配置SqlSessionFactoryBean
bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
property name="dataSource" ref="dataSource" />
bean>
4.7、配置自动扫描所有 Mapper 接口和文件
bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
property name="basePackage" value="cn.bdqn.mapper"/>
bean>
4.8、测试
@Test
public void testSpringAndMyBatis() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = (AccountService) ac.getBean("accountService");
ListAccount> accounts = accountService.queryAll();
System.out.println(accounts);
}
五、配置SpringMVC环境
5.1、主配置文件
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
context:component-scan base-package="cn.bdqn" use-default-filters="false">
context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
property name="prefix" value="/WEB-INF/jsp/"/>
property name="suffix" value=".jsp"/>
bean>
mvc:annotation-driven/>
beans>
5.2、web.xml配置前端控制器
web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
servlet>
servlet-name>frontDispatcherServletservlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
init-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:SpringMVC.xmlparam-value>
init-param>
load-on-startup>1load-on-startup>
servlet>
servlet-mapping>
servlet-name>frontDispatcherServletservlet-name>
url-pattern>/url-pattern>
servlet-mapping>
web-app>
六、Spring集成SpringMVC环境
在web.xml文件中配置。
listener>
listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
context-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:beans.xmlparam-value>
context-param>