整合步骤分析
SSM(Spring+SpringMVC+MyBatis)
-
Spring
-
框架基础
-
-
MyBatis
-
mysql+druid+pagehelper
-
-
Spring整合MyBatis
-
junit测试业务层接口
-
SpringMVC
-
rest风格(postman测试请求结果)
-
数据封装json(jackson)
-
-
Spring整合SpringMVC
-
Controller调用Service
-
-
其他
-
表现层数据封装
-
自定义异常
-
表结构
最重要的5个步骤
-
Spring
-
MyBatis
-
Spring整合MyBatis
-
SpringMVC
-
Spring整合SpringMVC
项目基础结构搭建
-
创建项目,组织项目结构,创建包
-
创建表与实体类
-
创建三层架构对应的模块、接口与实体类,建立关联关系
-
数据层接口(代理自动创建实现类)
-
业务层接口+业务层实现类
-
表现层类
-
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 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); }
Spring整合Mybatis
- 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>
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>
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>
整合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); } }