由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教。
一、代码结构
整个项目的代码结构如图所示:
controller为控制层,主要用于对业务模块的流程控制。
dao为数据接入层,主要用于与数据库进行连接,访问数据库进行操作,这里定义了各种操作数据库的接口。
mapper中存放mybatis的数据库映射配置。可以通过查看mybatis相关教程了解
model中存放了我们的实体类
service为业务层,我们的各种业务都定义在此,由controller调用不同业务实现不同的操作。
由于之前搭建环境都是自己配置依赖环境,导致缺各种缺包或者依赖冲突,所以这次我使用了maven来管理项目,可以上网查一下相关的教程,使用起来非常方便。
下面是我的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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
< 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.mjl</ groupId > < artifactId >testssm</ artifactId > < packaging >war</ packaging > < version >1.0-SNAPSHOT</ version > < name >testssm Maven Webapp</ name > < url >http://maven.apache.org</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < jackson.version >2.5.0</ jackson.version > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.12</ version > < scope >test</ scope > </ dependency > <!-- spring配置 --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-tx</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >${spring.version}</ version > < scope >test</ scope > </ dependency > <!-- mybatis 包 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.2.8</ version > </ dependency > <!--mybatis spring 插件 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.2.2</ version > </ dependency > <!-- mysql连接 --> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.34</ version > </ dependency > <!-- 数据源 --> < dependency > < groupId >com.mchange</ groupId > < artifactId >c3p0</ artifactId > < version >0.9.5-pre8</ version > </ dependency > < dependency > < groupId >org.aspectj</ groupId > < artifactId >aspectjweaver</ artifactId > < version >1.8.4</ version > </ dependency > <!-- log4j --> < dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >1.2.17</ version > </ dependency > <!-- servlet --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >servlet-api</ artifactId > < version >3.0-alpha-1</ version > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > </ dependency > <!-- json --> < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-mapper-asl</ artifactId > < version >1.9.13</ version > </ dependency > <!-- 文件上传 --> < dependency > < groupId >commons-io</ groupId > < artifactId >commons-io</ artifactId > < version >2.4</ version > </ dependency > < dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >1.2.2</ version > </ dependency > </ dependencies > < build > < finalName >testssm</ finalName > </ build > </ project > |
下面进入主题,直接上代码,我之前是先看了各种原理,但是看了半天也没明白,所以大家可以先把项目跑起来,然后再去对照springmvc的各个模块进行分析,哪个模块执行哪个操作,我认为这样的效果比较好。
我个人喜欢先从数据库与ORM框架与spirng开始搭建,这样的话在一开始就可以对ORM框架进行检验,免得在最后检验的时候出了问题。
先看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
|
<? xml version = "1.0" encoding = "utf-8" ?> < web-app xmlns = " http://java.sun.com/xml/ns/j2ee " version = "2.4" xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " > <!-- Spring配置 --> <!-- 配置Spring配置文件路径,好让ContextLoaderListener对其加载与解析--> < context-param > < param-name >contextConfigLocation</ param-name > < param-value > classpath*:config/applicationContext.xml </ param-value > </ context-param > <!-- 配置Spring上下文监听器,它的作用就是在启动WEB容器时,就会自动装在我们applicationContext.xml配置--> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- 配置Spring字符编码过滤器 --> < filter > < filter-name >encodingFilter</ 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 > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
配置完web.xml后,配置spring的applicationContext.xml,它是spring的配置文件,一般与spring集成的框架都要在这里进行配置。
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = " http://www.springframework.org/schema/beans " xmlns:context = " http://www.springframework.org/schema/context " 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--引入jdbc配置 --> < context:property-placeholder location = "classpath*:config/jdbc.properties" /> <!-- 扫描文件(自动将service层注入)--> < context:component-scan base-package = "com.mjl.service" /> <!--配置数据源--> < bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > < property name = "driverClass" value = "${jdbc_driverClassName}" /> < property name = "jdbcUrl" value = "${jdbc_url}" /> < property name = "user" value = "${jdbc_username}" /> < property name = "password" value = "${jdbc_password}" /> <!--连接池中保存的最大连接数目--> < property name = "maxPoolSize" value = "20" /> <!--连接池中保存的最少连接数目--> < property name = "minPoolSize" value = "2" /> <!-- 初始化连接大小 --> < property name = "initialPoolSize" value = "2" /> <!-- 获取连接最大等待时间 --> < property name = "maxConnectionAge" value = "6000" /> <!-- 连接池最大空闲 --> < property name = "maxIdleTime" value = "60" /> </ bean > <!--配置sqlSessionFactory 并将数据源注入--> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <!--注入数据源--> < property name = "dataSource" ref = "dataSource" /> <!--指定要使用到到mybatis配置文件--> < property name = "configLocation" value = "classpath:config/config.xml" /> <!--用于配置mapper映射xml--> < property name = "mapperLocations" value = "classpath*:com/mjl/mapper/*.xml" /> </ bean > <!-- 创建数据映射器--> < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.mjl.dao" /> </ bean > <!-- 对数据源进行事务管理 --> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > </ beans > |
在这里使用了jdbc.properties来分散配置,jdbc.properties中保存了数据库的信息,需要根据你们的数据库配置进行修改。
jdbc.properties
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3307/db_ssm?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=1234
然后是争对mybatis进行配置,由于我把数据库配置都配置在applicationcontext.xml中,所以我对mybatis中只进行了别名配置。
config.xml
1
|
2
3
4
5
6
7
8
9
10
11
|
<? 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 > <!--为com.mjl.model.User设置别名 User 方便调用--> < typeAliases > < typeAlias alias = "User" type = "com.mjl.model.User" /> </ typeAliases > </ configuration > |
配置完后,就开始进行数据库开发了,由于是一个简单的登录功能的实现,所以数据库也非常简单。
1
|
2
3
4
5
6
7
8
9
10
|
create database if not exists db_ssm character set utf8; use db_ssm; create table tb_user( id int (10) auto_increment, username varchar (20) not null , password varchar (20) not null , primary key (id) )ENGINE=InnoDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci; insert into tb_user(id,username, password ) values (1, 'alvin' ,1234); |
创建完数据库后开始写model下面的实体类
User.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
29
30
31
32
33
34
|
package com.mjl.model; /** * Created by alvin on 15/9/7. */ public class User { private int id; private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } } |
数据库接入层dao的接口,在这里我使用了mybatis以接口方式编程,这部分借鉴了其他的教程,有不懂的地方可以查看http://www.yihaomen.com/article/java/302.htm 该网址的mybatis教程,我觉得写的很不错。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.mjl.dao; import com.mjl.model.User; /** * Created by alvin on 15/9/7. * 此类为接口模式下的配置 */ public interface IUserDao { //这里以接口形式定义了数据库操作方法,我们只需 // 在Mybatis映射文件中对其进行映射就可以直接使用 public User selectById( int id); public User selectByName(String username); } |
mybatis映射文件文件名必须与接口类相同,否则无法映射成功。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? 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 "> <!--namespace用于与DAO层的接口类进行绑定,这样我们无需实现DAO层的接口 类,其接口类就能够自动的找到相关的SQL语句进行绑定实现--> < mapper namespace = "com.mjl.dao.IUserDao" > <!--select表示查询,它的id名称必须与DAO层接口的方法名相同,否则无法绑定--> < select id = "selectByName" parameterType = "string" resultType = "User" > select * from tb_user where username = #{username} </ select > < select id = "selectById" parameterType = "int" resultType = "User" > select * from tb_user where id = #{id} </ select > </ mapper > |
好了,到这里mybatis与spring已经整合完毕,我们需要测试一下mybatis是否与spring整合成功,写一个test类
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.mjl.test; import com.mjl.dao.IUserDao; import com.mjl.model.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Alvin on 15/9/6. */ public class Test { private static ApplicationContext ac; static { ac = new ClassPathXmlApplicationContext( "config/applicationContext.xml" ); } public static void main(String[] args) { IUserDao mapper = (IUserDao) ac.getBean( "IUserDao" ); System.out.println( "获取alvin" ); User user = mapper.selectByName( "alvin" ); System.out.println(user.getId()+ ":" + "username:" +user.getUsername()); System.out.println( "password:" +user.getPassword()); } } |
如果成功,如下图所示:
到这里mybatis与spring就整合结束了,明天继续更新下半部分。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。