SpringMVC入门二:SSM整合(spring+springmvc+mybatis)
一、编程步骤
1、引入依赖
spring、springmvc、mybatis、mybatis-spring、mysql、druid、log4j、servlet-api、jstl、fastjson
2、SM整合=Spring+mybatis
1)建表
2)实体类
3)DAO接口
public interface UserDao { List<User>findAll(); void save(User user); }
4)Mapper配置文件(位置:resources/mapper/UserDaoMapper.xml)
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.icucoder.dao.UserDao"> <select id="findAll" resultType="User"> select id, name, age, email from user; </select> <insert id="save" parameterType="User"> insert into user values (#{id}, #{name}, #{age}, #{email}); </insert> </mapper>
5)Service接口
public interface UserService { List<User> findAll(); void save(User user); }
6)Service实现类 @Service @Transactional 注入DAO对象
@Service("userService") @Transactional public class UserServiceImpl implements UserService { @Autowired UserDao userDao; public List<User> findAll() { return userDao.findAll(); } public void save(User user) { userDao.save(user); //int i = 1 / 0; System.out.println("save success"); } }
7)引入spring.xml文件,编写spring整合mybatis配置
(1)开启注解扫描<context:component-scan base-package="com.icucoder"/>
<context:component-scan base-package="com.icucoder"/>
(2)创建数据源对象DruidDataSource ,注入driverClassName url username passwrod
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="xroot"/> </bean>
(3)创建SqlSessionFactoryBean对象SqlSessionFactory ,注入dataSource,注入mapper配置文件位置mapperLocations
,注入typeAliasesPackage
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="typeAliasesPackage" value="com.icucoder.entity"/> </bean>
(4)创建DAO,使用MapperScannerConfigurer配置某路径下DAO自动扫描,注入sqlSessionFactoryBeanName配置
和注入sqlSessionFactory
basePackage
配置DAO接口所在包
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--扫描DAO接口所在包--> <property name="basePackage" value="com.icucoder.dao"/> </bean>
(5)创建事务管理器DataSourceTransactionManager
,注入DataSource
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
(6)开启注解式事务驱动<tx:annotation-driven transaction-manager="事务管理器在工厂中的唯一标识"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
8)测试Service方法调用
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) context.getBean("userService"); User user=new User(7,"test",12,"admin@qq.com"); userService.save(user); System.out.println(userService.findAll());
3、SS整合=Spring+SpringMVC
1)配置web.xml
(1)启动工厂监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
(2)配置工厂配置文件spring.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param>
(3)配置springmvc核心的Servlet:DispatcherServlet,url-pattern /,并在init-param中配置
contextConfigLocation为
classpath:springmvc.xml
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
(4)配置springmvc接收post请求中文乱码 CharacterEncodingFilter usl-pattern /* init-param encoding UTF-8
<filter> <filter-name>charset</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>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2)引入springmvc.xml
(1)开启注解扫描controller
<context:component-scan base-package="com.icucoder.controller"/>
(2)配置处理器映射器和处理器适配器
<mvc:annotation-driven/>
(3)配置视图解析器
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean>
3)开发Controller并使用@Controller和@RequestMapping,注入Service
(1)controller
@Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/all") public String getUser(HttpServletRequest request) { List<User> users = userService.findAll(); request.setAttribute("users", users); return "index"; } }
(2)jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>all users info</h3> <c:forEach items="${requestScope.users}" var="user"> ${user.id} & ${user.name} </c:forEach> </body> </html>
4)服务器附属并访问测试
http://localhost:8080/spring_sprinmvc_mybatis_war/user/all
二、说明
1、现有SSM开发存在的问题
1)大量maven冗余配置
2)每次构建项目都要书写大量相同配置极大浪费了项目开发时间
3)每次整合第三方技术都要编写相关配置文件
4)项目测试每次都需要部署到tomcat
如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
![qq群](https://files-cdn.cnblogs.com/files/hoaprox/qqqun.bmp)
![微信](https://files.cnblogs.com/files/hoaprox/zsm.bmp)