SSM整合
整合思路
- 各自搭建SSM环境
- 使用Spring整合Mybatis
- 使用Spring整合SpringMVC
搭建Mybatis环境
创建工程
Maven项目-web工程
引入mybatis坐标
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- 辅助-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
创建pojo
public class Account {
private Integer aid;
private String name;
private Float balance;
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getBalance() {
return balance;
}
public void setBalance(Float balance) {
this.balance = balance;
}
}
创建接口IAccountDao
public interface IAccountDao {
List<Account> findAll();
}
创建dao映射文件sql
<mapper namespace="com.itheima.dao.IAccountDao">
<select id="findAll" resultType="account">
SELECT * FROM tb_account;
</select>
</mapper>
加入主配置文件
mybatis-config.xml
<configuration>
<!-- 引入数据库外部配置文件-->
<properties resource="db.properties"/>
<!-- 引入别名-->
<typeAliases>
<package name="com.itheima.domain"/>
</typeAliases>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.itheima.dao"/>
</mappers>
</configuration>
- db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_lsn
jdbc.user=root
jdbc.password=xml123xml
- 测试
@Test
public void testAccountDao() {
//读取配置文件
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
IAccountDao accountDao = sqlSession.getMapper(IAccountDao.class);
List<Account> accounts = accountDao.findAll();
accounts.stream().forEach(account -> System.out.println(account));
} catch (IOException e) {
e.printStackTrace();
}
}
搭建Spring的环境
- 创建IAccountService
public interface IAccountService {
List<Account> findAll();
}
- 创建IAccountServiceImpl实现类
public class AccountService implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
}
- 测试
@Override
public List<Account> findAll() {
System.out.println("findAll");
return null;
// return accountDao.findAll();
}
搭建SpringMVC环境
- 引入坐标
<!--springMVC 相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- 加入springmvc的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置注解扫描-->
<context:component-scan base-package="com.itheima.controller"/>
<!-- 配置三大组件-->
<mvc:annotation-driven/>
<!-- 配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 加入web配置文件
<?xml version="1.0" encoding="utf-8"?>
<web-app version="3.0"
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">
<!-- 配置前端适配器-->
<servlet>
<servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置中文乱码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 请求页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/account/list">账户列表</a>
</body>
</html>
- 控制器
/**
* @author wangxin
* @date 2019/12/15 16:48
* @description: TODO
* GOOD LUCK!
*/
@Controller
public class AccountController {
@RequestMapping("/account/list")
public String list(Model model) {
model.addAttribute("list", "list");
return "list";
}
}
- 响应页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${list}
</body>
</html>
Spring整合Mybatis
整合思路
将Mybatis的SqlSessionFactory托管到Spring的IOC容器中
将mybatis的所有配置信息转移到Spring的配置文件中
mybatis提供一个对接spring的中间包
加入整合包
<!--mybatis整合Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
将mybatis的所有配置文件转移到spring的配置文件中
<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"
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">
<!-- 注解扫描-->
<context:component-scan base-package="com.itheima.service"/>
<!-- 引入外部配置-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.itheima.domain"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 引入mapper的配置文件-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
</beans>
- 将dao注入到Service
@Service
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
List<Account> accounts = accountDao.findAll();
System.out.println(accounts);
return null;
// return accountDao.findAll();
}
}
Spring整合SpringMVC
Spring和SpringMVC是同一家产品,是不需要整合的
但是现在的Spring容器自己无法启动,我们需要在启动Web容器的时候加载Spring的配置文件,启动Spring容器那么这个工作是在spring-web包中监听器来做的,这个包不用单独导入,他已经在Spring-webmvc包中了,它会监听WEB容器的启动和停止,然后就可以控制Spring容器的启动和停止了
- 配置监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 向SpringMVC中注入service
@Service
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
List<Account> accounts = accountDao.findAll();
System.out.println(accounts);
return null;
// return accountDao.findAll();
}
}
网友评论