前言
说起整合自然离不开ssm,我本身并不太喜欢ORM,尤其是MyBatis,把SQL语句写在xml里,尤其是大SQL,可读性不高,出错也不容易排查。
开发环境
idea2016、SpringMVC4、Mybatis3
项目结构
SSM整合
1、pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.autohome</groupId> 5 <artifactId>SpringMVC3</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>SpringMVC3</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>4.10</version> 15 </dependency> 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-core</artifactId> 19 <version>4.3.6.RELEASE</version> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-beans</artifactId> 24 <version>4.3.6.RELEASE</version> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-context</artifactId> 29 <version>4.3.6.RELEASE</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-web</artifactId> 34 <version>4.3.6.RELEASE</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-context-support</artifactId> 39 <version>4.3.6.RELEASE</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-webmvc</artifactId> 44 <version>4.3.6.RELEASE</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-jdbc</artifactId> 49 <version>4.3.6.RELEASE</version> 50 </dependency> 51 <dependency> 52 <groupId>org.apache.velocity</groupId> 53 <artifactId>velocity</artifactId> 54 <version>1.6.2</version> 55 </dependency> 56 <dependency> 57 <groupId>org.apache.velocity</groupId> 58 <artifactId>velocity-tools</artifactId> 59 <version>2.0</version> 60 </dependency> 61 <dependency> 62 <groupId>org.mybatis</groupId> 63 <artifactId>mybatis</artifactId> 64 <version>3.4.2</version> 65 </dependency> 66 <dependency> 67 <groupId>org.mybatis</groupId> 68 <artifactId>mybatis-spring</artifactId> 69 <version>1.3.0</version> 70 </dependency> 71 <dependency> 72 <groupId>com.microsoft.sqlserver</groupId> 73 <artifactId>sqljdbc4</artifactId> 74 <version>4.0</version> 75 </dependency> 76 <dependency> 77 <groupId>commons-dbcp</groupId> 78 <artifactId>commons-dbcp</artifactId> 79 <version>1.4</version> 80 </dependency> 81 </dependencies> 82 <build> 83 <finalName>SpringMVC3</finalName> 84 </build> 85 </project>
2、web.xml
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 <!--告知javaEE容器,有那些内容需要添加到上下文里去--> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:applicationContext.xml</param-value> 11 </context-param> 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 <!--spring 前端控制器--> 16 <servlet> 17 <servlet-name>SpringMVC</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <init-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value>classpath:springmvc-servlet.xml</param-value> 22 </init-param> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>SpringMVC</servlet-name> 26 <url-pattern>/</url-pattern> 27 </servlet-mapping> 28 </web-app>
3、applicationContext.xml无配置内容所以忽略
4、springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd 12 "> 13 14 <!--从配置文件加载数据库信息--> 15 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 16 <property name="locations" value="classpath:config/jdbc.properties"/> 17 <property name="fileEncoding" value="UTF-8"/> 18 </bean> 19 20 <!--配置数据源,这里使用Spring默认--> 21 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 22 <property name="driverClassName" value="${sqlserver.driver}"/> 23 <property name="url" value="${sqlserver.url}"/> 24 <property name="username" value="${sqlserver.username}"/> 25 <property name="password" value="${sqlserver.password}"/> 26 </bean> 27 28 <!--扫描Mapper--> 29 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 30 <property name="basePackage" value="com.autohome.mapper"/> 31 32 </bean> 33 34 <!--配置sqlSessionFactory--> 35 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 36 <property name="configLocation" value="classpath:springmvc-mybatis.xml"/> 37 <property name="dataSource" ref="dataSource"/> 38 </bean> 39 40 <!--启用最新的注解器、映射器--> 41 <mvc:annotation-driven/> 42 43 <!--扫描Controller注解类--> 44 <context:component-scan base-package="com.autohome.controller" /> 45 <!--扫描Service注解类--> 46 <context:component-scan base-package="com.autohome.service"/> 47 48 <!--配置视图解析器--> 49 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 50 <property name="prefix" value="/WEB-INF/views/"/> 51 <property name="suffix" value=".jsp"/> 52 </bean> 53 54 </beans>
5、springmvc-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 6 <!-- 实体类,简称 -设置别名 --> 7 <typeAliases> 8 <typeAlias alias="User" type="com.autohome.model.User" /> 9 </typeAliases> 10 11 12 <mappers> 13 <mapper resource="mapper/UserMapper.xml" /> 14 </mappers> 15 16 </configuration>
6、dao接口层、mapper(dao接口实现层)、Biz层、 model层忽略不计(id,name,address3个测试字段)。 mapper文件让我踩了坑,后恍然大悟,mapper.xml要放在resources包下。
public interface UserMapper { List<User> listAllUser(); List<User> listPagedUser(@Param("pageIndex") int pageIndex,@Param("pageSize") int pageSize); int count(); int updateUser(User user); int deleteUser(int id); int insertUser(User user); User getUserById(int id); }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!--命名空间和接口保持一致--> 5 <mapper namespace="com.autohome.mapper.UserMapper"> 6 <select id="listAllUser" resultType="User"> 7 select * from t_userinfo 8 </select> 9 10 <select id="listPagedUser" resultType="User"> 11 select top ${pageSize} * from t_userinfo where id not in (select top (${pageSize} * (${pageIndex} -1)) id from t_userinfo) 12 </select> 13 14 <select id="count" resultType="int"> 15 select count(*) from t_userinfo 16 </select> 17 18 <insert id="insertUser" parameterType="User"> 19 insert into t_userinfo VALUES (#{name},#{address}) 20 </insert> 21 22 <update id="updateUser" parameterType="User"> 23 UPDATE t_userinfo set name=#{name},address=#{address} where id=#{id} 24 </update> 25 26 <delete id="deleteUser" parameterType="int"> 27 DELETE FROM t_userinfo where id=#{id} 28 </delete> 29 30 <select id="getUserById" resultType="User" parameterType="int"> 31 select * from t_userinfo where id=#{id} 32 </select> 33 34 </mapper>
*/ public interface IUserBiz { List<User> listAllUser(); List<User> listPagedUser(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize); int count(); int updateUser(User user); int deleteUser(int id); int insertUser(User user); User getUserById(int id); }
package com.autohome.service; import com.autohome.model.User; import com.autohome.mapper.UserMapper; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class UserBizImpl implements IUserBiz { @Autowired private UserMapper userMapper; public List<User> listAllUser() { return userMapper.listAllUser(); } public List<User> listPagedUser(@Param("pageIndex") int pageIndex,@Param("pageSize") int pageSize) { return userMapper.listPagedUser(pageIndex,pageSize); } public int count() { return userMapper.count(); } public int updateUser(User user) { return userMapper.updateUser(user); } public int deleteUser(int id) { return userMapper.deleteUser(id); } public int insertUser(User user) { return userMapper.insertUser(user); } public User getUserById(int id) { return userMapper.getUserById(id); } }
7、Controller。 我新建了一个UserController,在这里调用了增删改查分页的操作
package com.autohome.controller; import com.autohome.service.IUserBiz; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.autohome.model.User; @Controller @RequestMapping("/User") public class UserController { @Autowired private IUserBiz userBiz; @RequestMapping("/index") public ModelAndView index(){ //System.out.println("size:"+userBiz.listAllUser().size()); System.out.println("size:"+userBiz.count()); // // User user =new User(); // user.setName("张三"); // user.setAddress("shanxi"); // // int result = userBiz.insertUser(user); // if(result>0) // { // System.out.println("insert success"); // }else{ // System.out.println("insert err"); // } int result = userBiz.deleteUser(39); if(result>0) { System.out.println("delete success"); }else{ System.out.println("delete err"); } // User user =new User(); // user.setId(35); // user.setName("张三11111"); // user.setAddress("china"); // // int result = userBiz.updateUser(user); // if(result>0) // { // System.out.println("update success"); // }else{ // System.out.println("update err"); // } //System.out.println("size:"+userBiz.listPagedUser(1,10).size()); ModelAndView mav=new ModelAndView("index"); return mav; } }
总结
做这个demo前我看的ssm整合教程全部是基于myeclipse开发的,而且教程把dao接口和dao实现是全部放在src java目录下的,也就是mapper目录包括了mapper接口和mapper.xml。 我做第一个demo时在idea里也是这么做的,demo运行始终不成功,一直提示找不 到mapper.xml里的方法,后来编译的时候我发现target/classes里确实找不到mapper.xml。 不知道用myeclipse整合开发时是否遇到这个问题,后我把mapper.xml文件放到resources目录中,编译后target文件总就能找到mapper.xml。 方法运行也搞定了。写demo写了半个小时,debug这个问题花了2个小时,好在demo跑起来了,也算是有收获的。