ssm整合
1. 1整合流程简介
整合步骤分析
SSM(Spring+SpringMVC+MyBatis)
- Spring
框架基础 - MyBatis
mysql+druid+pagehelper - Spring整合MyBatis
- Junit测试业务层接口
- SpringMVC
rest风格(postman测试请求结果)
数据封装json(jackson) - Spring整合SpringMVC
Controller调用Service - 其他
表现层数据封装
自定义异常
最重要的5个步骤
1、Spring
2、MyBatis
3、Spring整合MyBatis
4、SpringMVC
5、Spring整合SpringMVC
1. 2项目结构搭建
Part0:项目基础结构搭建
- 创建项目,组织项目结构,创建包
- 创建表与实体类
- 创建三层架构对应的模块、接口与实体类,建立关联关系
- 数据层接口(代理自动创建实现类)
业务层接口+业务层实现类
表现层类
public interface UserDao {
public boolean save(User user);
public boolean update(User user);
public boolean delete(Integer uuid);
public User get(Integer uuid);
public List<User> getAll(int page,int size);
//注意:数据层操作不要和业务层操作的名称混淆,通常数据层仅反映与数据库间的信息交换,不体现业务逻辑
public User getByUserNameAndPassword(@Param("userName") String userName, @Param("password") String password);
}
public interface UserService {
public boolean save(User user);
public boolean update(User user);
public boolean delete(Integer uuid);
public User get(Integer uuid);
public List<User> getAll(int page, int size);
/**
用户登录
@param userName 用户名
@param password 密码信息
@return
*/
public User login(String userName,String password);
}
1. 3Spring整合Mybatis
Part1: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">
<!--开启bean注解扫描-->
<context:component-scan base-package="com.itheima"/>
</beans>
Part1:MyBatis配置事务
MyBatis映射
<?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="com.itheima.dao.UserDao">
<!--添加-->
<insert id="save" parameterType="user">
insert into user(userName,password,realName,gender,birthday)values(#{userName},#{password},#{realName},#{gender},#{birthday})
</insert>
<!--删除-->
<delete id="delete" parameterType="int">
delete from user where uuid = #{uuid}
</delete>
<!--修改-->
<update id="update" parameterType="user">
update user set userName=#{userName},password=#{password},realName=#{realName},gender=#{gender},birthday=#{birthday} where uuid=#{uuid}
</update>
<!--查询单个-->
<select id="get" resultType="user" parameterType="int">
select * from user where uuid = #{uuid}
</select>
<!--分页查询-->
<select id="getAll" resultType="user">
select * from user
</select>
<!--登录-->
<select id="getByUserNameAndPassword" resultType="user" >
select * from user where userName=#{userName} and password=#{password}
</select>
</mapper>
Spring整合Mybatis核心配置
<!--开启注解式事务-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--加载properties文件-->
<context:property-placeholder location="classpath*:jdbc.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.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--整合mybatis到spring中-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.itheima.domain"/>
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--映射扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
1. 4整合junit
Part2:单元测试整合junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testDelete(){
User user = new User(); userService.delete(3);
}
}
1. 4Spring整合SpringMVC
Part3:SpringMVC
web.xml配置
<!--告诉服务器上下文参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--启动服务器时,通过监听器加载spring运行环境,把Spring的核心配置文件加载到servletContext上下文中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
spring-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--注解驱动-->>
<mvc:annotation-driven/>
<context:component-scan base-package="com.itheima.controller"/>
</beans>
controller层
@RestController
@RequestMapping("/user") public class UserController {
@PostMapping
public boolean save(User user) {
System.out.println("save ..." + user); return true;
}
@PostMapping("/login")
public User login(String userName,String password){
System.out.println("login ..." + userName + " ," +password);
return null;
}
}
Part4:Spring整合SpringMVC
web.xml加载Spring环境
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--启动服务器时,通过监听器加载spring运行环境-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Controller调用Service
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public boolean save(User user){
return userService.save(user);
}
}