由于本人愚鈍,整合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就整合結束了,明天繼續更新下半部分。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。