前言
說起整合自然離不開ssm,我本身并不太喜歡ORM,尤其是MyBatis,把SQL語句寫在xml里,尤其是大SQL,可讀性不高,出錯(cuò)也不容易排查。
開發(fā)環(huán)境
idea2016、SpringMVC4、Mybatis3
項(xiàng)目結(jié)構(gòu)
SSM整合
1、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
|
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.autohome</ groupId > < artifactId >SpringMVC3</ artifactId > < packaging >war</ packaging > < version >1.0-SNAPSHOT</ version > < name >SpringMVC3</ name > < url >http://maven.apache.org </ url > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.10</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context-support</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >4.3.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.apache.velocity</ groupId > < artifactId >velocity</ artifactId > < version >1.6.2</ version > </ dependency > < dependency > < groupId >org.apache.velocity</ groupId > < artifactId >velocity-tools</ artifactId > < version >2.0</ version > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.4.2</ version > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.3.0</ version > </ dependency > < dependency > < groupId >com.microsoft.sqlserver</ groupId > < artifactId >sqljdbc4</ artifactId > < version >4.0</ version > </ dependency > < dependency > < groupId >commons-dbcp</ groupId > < artifactId >commons-dbcp</ artifactId > < version >1.4</ version > </ dependency > </ dependencies > < build > < finalName >SpringMVC3</ finalName > </ build > </ project > |
2、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
|
<!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 > < display-name >Archetype Created Web Application</ display-name > <!--告知javaEE容器,有那些內(nèi)容需要添加到上下文里去--> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </ context-param > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!--spring 前端控制器--> < 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-servlet.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >SpringMVC</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > </ web-app > |
3、applicationContext.xml無配置內(nèi)容所以忽略
4、springmvc-servlet.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
|
<? 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:mvc = " http://www.springframework.org/schema/mvc " 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--從配置文件加載數(shù)據(jù)庫信息--> < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name = "locations" value = "classpath:config/jdbc.properties" /> < property name = "fileEncoding" value = "UTF-8" /> </ bean > <!--配置數(shù)據(jù)源,這里使用Spring默認(rèn)--> < bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > < property name = "driverClassName" value = "${sqlserver.driver}" /> < property name = "url" value = "${sqlserver.url}" /> < property name = "username" value = "${sqlserver.username}" /> < property name = "password" value = "${sqlserver.password}" /> </ bean > <!--掃描Mapper--> < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.autohome.mapper" /> </ bean > <!--配置sqlSessionFactory--> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "configLocation" value = "classpath:springmvc-mybatis.xml" /> < property name = "dataSource" ref = "dataSource" /> </ bean > <!--啟用最新的注解器、映射器--> < mvc:annotation-driven /> <!--掃描Controller注解類--> < context:component-scan base-package = "com.autohome.controller" /> <!--掃描Service注解類--> < context:component-scan base-package = "com.autohome.service" /> <!--配置視圖解析器--> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/WEB-INF/views/" /> < property name = "suffix" value = ".jsp" /> </ bean > </ beans > |
5、springmvc-mybatis.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" " http://mybatis.org/dtd/mybatis-3-config.dtd "> < configuration > <!-- 實(shí)體類,簡稱 -設(shè)置別名 --> < typeAliases > < typeAlias alias = "User" type = "com.autohome.model.User" /> </ typeAliases > < mappers > < mapper resource = "mapper/UserMapper.xml" /> </ mappers > </ configuration > |
6、dao接口層、mapper(dao接口實(shí)現(xiàn)層)、Biz層、 model層忽略不計(jì)(id,name,address3個(gè)測試字段)。 mapper文件讓我踩了坑,后恍然大悟,mapper.xml要放在resources包下。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
|
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
|
<?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.autohome.mapper.UserMapper" > <select id= "listAllUser" resultType= "User" > select * from t_userinfo </select> <select id= "listPagedUser" resultType= "User" > select top ${pageSize} * from t_userinfo where id not in (select top (${pageSize} * (${pageIndex} - 1 )) id from t_userinfo) </select> <select id= "count" resultType= "int" > select count(*) from t_userinfo </select> <insert id= "insertUser" parameterType= "User" > insert into t_userinfo VALUES (#{name},#{address}) </insert> <update id= "updateUser" parameterType= "User" > UPDATE t_userinfo set name=#{name},address=#{address} where id=#{id} </update> <delete id= "deleteUser" parameterType= "int" > DELETE FROM t_userinfo where id=#{id} </delete> <select id= "getUserById" resultType= "User" parameterType= "int" > select * from t_userinfo where id=#{id} </select> </mapper> |
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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); } |
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
|
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。 我新建了一個(gè)UserController,在這里調(diào)用了增刪改查分頁的操作
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
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; } } |
總結(jié)
做這個(gè)demo前我看的ssm整合教程全部是基于myeclipse開發(fā)的,而且教程把dao接口和dao實(shí)現(xiàn)是全部放在src java目錄下的,也就是mapper目錄包括了mapper接口和mapper.xml。 我做第一個(gè)demo時(shí)在idea里也是這么做的,demo運(yùn)行始終不成功,一直提示找不 到mapper.xml里的方法,后來編譯的時(shí)候我發(fā)現(xiàn)target/classes里確實(shí)找不到mapper.xml。 不知道用myeclipse整合開發(fā)時(shí)是否遇到這個(gè)問題,后我把mapper.xml文件放到resources目錄中,編譯后target文件總就能找到mapper.xml。 方法運(yùn)行也搞定了。寫demo寫了半個(gè)小時(shí),debug這個(gè)問題花了2個(gè)小時(shí),好在demo跑起來了,也算是有收獲的。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://www.cnblogs.com/sword-successful/p/6486339.html