ssm框架整合。具体流程:
- 先整合spring + mybatis。
- 整合完成后测试。
- 正常就去整合spring mvc。
- 最后再进行测试。
从零开始项目,我们来做一个简单的登录实例。
一、项目结构
二、环境配置
(1)首先是根据需求设计数据库
- 有人习惯先设计接口,再根据接口设计数据库,这是接口驱动模式。
- 也有人习惯先设计测试用例,再设计接口,再设计数据库,这是测试驱动模式。
我这里先设计数据库,只是为了演示,真正的开发流程还是从接口开始比较好。
(2)根据数据库生成mybatis代码
使用mybatis generator,生成相应文件。
应该有三个文件:
- dao中的mapper文件:userMapper.java。
- 对应mapper的sql语句配置文件:userMapper.xml。
- pojo:user.java。
dao中的mapper文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.xie.dao; import com.xie.pojo.user; public interface userMapper { int deleteByPrimaryKey(Integer id); int insert(user record); int insertSelective(user record); user selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(user record); int updateByPrimaryKey(user record); user login(user user); } |
我加上了login这个映射方法。
对应mapper的sql语句配置文件userMapper.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?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.xie.dao.userMapper" > <resultMap id="BaseResultMap" type="com.xie.pojo.user" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, username, password </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from user where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.xie.pojo.user" > insert into user (id, username, password ) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.xie.pojo.user" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="password != null" > password, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.xie.pojo.user" > update user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.xie.pojo.user" > update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <select id="login" parameterType="com.xie.pojo.user" resultType="com.xie.pojo.user"> select * from user where username = #{username} and password = #{password} </select> </mapper> |
在sql语句配置文件中加上login方法映射的sql语句。
相应的pojo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.xie.pojo; public class user { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } @Override public String toString() { return "user [id=" + id + ", username=" + username + ", password=" + password + "]"; } } |
为了显示类比较方便,我加了toString()方法。
(3)建立Maven项目
- 在library中加入runtime环境,解决报错。
- 在Project Facets中更改各种版本信息,比如java改成你安装的版本,动态网页版本改成3.0,如果eclipse中无法修改,那么就找到项目所在目录,更改org.eclipse.wst.common.project.facet.core.xml文件。
- 建立相关包,把刚才generator生成的文件放进去。
(4)配置pom.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> <scope>system</scope> <!--本地jar的路径,相对或者绝对都可以 --> <systemPath>E:\谢宏滔\方法记忆\常用包\gson-2.6.2.jar</systemPath> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-juli</artifactId> <version>7.0.65</version> </dependency> </dependencies> |
这里的依赖基本上够用了,有一些是没必要加的,可以进行删改。
到这一步,环境基本上就配置好了。接下来是重头戏。
二、先整合spring和mybatis
先整合spring和mybatis。
(1)配置spring配置文件spring.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <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-4.2.xsd"> <context:component-scan base-package="com.xie.service"></context:component-scan> </beans> |
因为等下我们要写一个服务类service,需要放进spring ioc容器中,所以只是配置了一个自动扫描注解类。
(2)配置spring与mybatis的整合文件spring-mybatis.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?xml version="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx" 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-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 1.数据源: DriverManagerDataSource --> <bean name="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 2.mybatis的SqlSession 的工厂: SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:com/xie/mapper/*.xml"></property> </bean> <!-- 3.mybatis自动扫描加载Sql 映射文件 : MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xie.dao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 4.事务管理: DataSourceTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 5.使用声明式事务 --> <tx:annotation-driven transaction-manager="txManager" /> </beans> |
这个是整合的核心。
这样,spring和mybatis的整合就基本完成了。下面进行测试。
(3)写测试方法
1.创建一个service包
2.写一个接口userservice.java
1 2 3 4 5 6 7 |
package com.xie.service; import com.xie.pojo.user; public interface userservice { user login(user user); } |
(3)写一个实现类userserviceimpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.xie.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xie.dao.userMapper; import com.xie.pojo.user; @Service("userservice") public class userserviceimpl implements userservice { @Autowired private userMapper userMapper; public user login(user user) { return userMapper.login(user); } } |
(4)写一个测试类test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.xie; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xie.pojo.user; import com.xie.service.userservice; public class test { private userservice userservice; @Test public void test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring-mybatis.xml"}); userservice = (com.xie.service.userservice) applicationContext.getBean("userservice"); user user = new user(); user.setUsername("yxin"); user.setPassword("456"); user result = userservice.login(user); System.out.println(result.toString()); } } |
如果查询错误,就要具体问题具体解决了,其实这步才是最繁琐的。
如果能正确查询出这个类,就说明spring和mybatis已经整合完成。
三、整合spring mvc
一定要在上面的步骤都完成之后,最后才来整合spring mvc。
(1)首先配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/spring.xml,classpath:/spring/spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app> |
最重要的是设置监听器,从而在spring mvc启动时加载spring.xml和spring-mybatis.xml配置文件。
(2)配置spring-mvc.xml
用于spring mvc找controller。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <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-4.2.xsd"> <context:component-scan base-package="com.xie.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> |
因为controller用的是@ResponseBody,所以不需要用到视图解析器,这里还是配了。
(3)写一个控制器
新建一个controller包,写一个控制器类login.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.xie.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.xie.pojo.user; import com.xie.service.userservice; @Controller public class login { @ResponseBody @RequestMapping(value = "/login", method = RequestMethod.GET, params = {"username","password"}) public String login(user user){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring-mybatis.xml"}); userservice userservice = (com.xie.service.userservice) applicationContext.getBean("userservice"); if (userservice.login(user) != null) { return "success"; } else{ return "fail"; } } } |
需要手动去从ioc容器中获取userservice类。
或者@Autowired注入userservice实现类也可以:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.xie.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.xie.pojo.user; import com.xie.service.userservice; @Controller public class login { @Autowired userservice userservice; @ResponseBody @RequestMapping(value = "/login", method = RequestMethod.GET, params = { "username", "password" }) public String login(user user){ if (userservice.login(user) != null) { return "success"; }else { return "fail"; } } } |
(4)进行测试
访问:http://localhost:8080/5.7springmvc/login?username=xie&password=123
那就是成功了。至此ssm框架整合完成。
(5)加强细节
- get请求变成post。
- 写个html页面进行ajax获取。
- …
四、总结
总之,先整合spring + mybatis,整合完成后测试,正常就去整合spring mvc,最后再进行测试。