maven ssm框架 mysql_用Maven整合SSM框架

前述

Maven 是专门用于构建和管理Java相关项目的工具,利用 Maven 的主要目的是统一维护 jar 包。关于 Maven 的安装在这篇里面就不说了。

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。关于SSM框架的基本思想这里也不赘述了,主要是记录 Maven 整合 SSM 的过程。

借用之前学习的课本上的一段话,讲述SSM 的整合思路:

由于Spring MVC是Spring框架中的一个模块,所以Spring MVC与Spring之间不存在整合的问题,只要引入相应JAR包就可以直接使用。因此SSM框架的整合就只涉及到了Spring与MyBatis的整合,以及Spring MVC与MyBatis的整合。Spring与MyBatis框架的整合时,

通过Spring实例化Bean,然后调用实例对象中的查询方法来执行MyBatis映射文件中的SQL语句的,如果能够正确查询出数据库中的数据,那么就可以认为Spring与MyBatis框架整合成功。加入Spring MVC后,如果可以通过前台页面来执行查询方法,并且查询出的数据能

够在页面中正确显示,那么就可以认为三大框架整合成功。

整体项目结构

7382dd9f1da85522976f43a9d2958d64.png

创建Maven项目

新建Maven项目,选择Maven Project

5eb96a89dfe12ce6f412c0588da8516e.png

默认下一步

45792abed8163ed688fe23c945ccb756.png

选择webapp,下一步

5d27dadd4fe3e49d36b889dc9eb3289a.png

设置GroupID和ArtifactID

adeddf03657a44fd3a8edad04eaf2c45.png

项目创建完成

86f6ac44c34b4754daea1e80f70ac71c.png

有报错,打开项目属性,找到Java Build Path -> Libraries ->Edit ->Workspace default

25b7a9a1d3a4c60b46e3cfd4925186b4.png

4d49911917280acb8710ec22b03832db.png

问题解决

fd072456a1169baeea65f135f2e50002.png

利用Maven导入jar包

前述中我也说了,利用 Maven 的目的就是要统一维护 jar 包,而jar包的维护则是通过一个 pom.xml 文件来实现的,我们可以在目录中看到一个 pom.xml 文件,项目的 jar 包也正是要通过这个文件导入。

5865338f2d85f9618243bca4b866cbcd.png

1

2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

3 4.0.0

4 com.cjk

5 ssm

6 war

7 0.0.1-SNAPSHOT

8 ssm Maven Webapp

9 http://maven.apache.org

10

11

12

13 4.0.2.RELEASE

14

15 3.2.6

16

17 1.7.7

18 1.2.17

19

20

21

22 junit

23 junit

24 3.8.1

25 test

26

27

28

29 javax

30 javaee-api

31 7.0

32

33

34 junit

35 junit

36 4.11

37

38 test

39

40

41

42 org.springframework

43 spring-core

44 ${spring.version}

45

46

47 org.springframework

48 spring-web

49 ${spring.version}

50

51

52 org.springframework

53 spring-oxm

54 ${spring.version}

55

56

57 org.springframework

58 spring-tx

59 ${spring.version}

60

61

62 org.springframework

63 spring-jdbc

64 ${spring.version}

65

66

67 org.springframework

68 spring-webmvc

69 ${spring.version}

70

71

72 org.springframework

73 spring-aop

74 ${spring.version}

75

76

77 org.springframework

78 spring-context-support

79 ${spring.version}

80

81

82 org.springframework

83 spring-test

84 ${spring.version}

85

86

87

88 org.mybatis

89 mybatis

90 ${mybatis.version}

91

92

93

94 org.mybatis

95 mybatis-spring

96 1.2.2

97

98

99

100 mysql

101 mysql-connector-java

102 5.1.30

103

104

105

106 commons-dbcp

107 commons-dbcp

108 1.2.2

109

110

111

112 jstl

113 jstl

114 1.2

115

116

117

118

119 log4j

120 log4j

121 ${log4j.version}

122

123

124

125 com.alibaba

126 fastjson

127 1.1.41

128

129

130 org.slf4j

131 slf4j-api

132 ${slf4j.version}

133

134

135 org.slf4j

136 slf4j-log4j12

137 ${slf4j.version}

138

139

140

141

142 org.codehaus.jackson

143 jackson-mapper-asl

144 1.9.13

145

146

147

148 commons-fileupload

149 commons-fileupload

150 1.3.1

151

152

153 commons-io

154 commons-io

155 2.4

156

157

158 commons-codec

159 commons-codec

160 1.9

161

162

163

164 org.mybatis.generator

165 mybatis-generator-core

166 1.3.6

167

168

169

170

171

172 ssm

173

174

175 org.apache.maven.plugins

176 maven-compiler-plugin

177 3.5.1

178

179 1.8

180 1.8

181 UTF-8

182

183

184

185

186

建立 jdbc 属性配置文件, db.properties

1 jdbc.driver=com.mysql.jdbc.Driver2 jdbc.url=jdbc:mysql://localhost:3306/mybatis3 jdbc.username=root4 jdbc.password=密码

建立 log4j 日志配置文件, log4j.properties

1 #定义LOG输出级别2 log4j.rootLogger=INFO,Console,File3 #定义日志输出目的地为控制台4 log4j.appender.Console=org.apache.log4j.ConsoleAppender5 log4j.appender.Console.Target=System.out6 #可以灵活地指定日志输出格式,下面一行是指定具体的格式7 log4j.appender.Console.layout = org.apache.log4j.PatternLayout8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n9 #文件大小到达指定尺寸的时候产生一个新的文件10 log4j.appender.File = org.apache.log4j.RollingFileAppender11 #指定输出目录12 log4j.appender.File.File = logs/springmvcMybist/ssm.log13 #定义文件最大大小14 log4j.appender.File.MaxFileSize = 10MB15 #输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志16 log4j.appender.File.Threshold = ALL17 log4j.appender.File.layout = org.apache.log4j.PatternLayout

建立 applicationContext.xml 配置文件

applicationContext.xml 是spring 的配置文件,读取 db.properties 文件的配置和数据源配置,配置事务管理器、开启事务注解。配置用于整合 Mybatis 框架的 Mybatis 工厂信息,定义 mapper 扫描器来扫描 DAO 和 Service 层的配置。

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"

4 xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

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

建立 Mybatis 配置文件, mybatis-config.xml

因为已经在 Spring 中配置了数据源信息以及 mapper 接口文件扫描器,所以在 Mybatis 的配置文件中只需要根据 POJO 类路径进行别名配置。

1 <?xml version="1.0" encoding="UTF-8"?>

2 "http://mybatis.org/dtd/mybatis-3-config.dtd">

4

5

6

7

8

9

建立 Spring MVC 的配置文件,springmvc-config.xml

配置用于扫描 @Controller 注解的包扫描器、注解驱动器以及视图解析器。

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

4 xmlns:context="http://www.springframework.org/schema/context"

5 xmlns:mvc="http://www.springframework.org/schema/mvc"

6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

9

10

11

12

13

14

15

16

17

18

修改 web.xml 文件

配置 spring 的文件监听器、编码过滤器以及 Spring MVC 的前端控制器等信息。

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns="http://java.sun.com/xml/ns/javaee"

4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

5 version="3.0">

6 Archetype Created Web Application

7

8

9

10 contextConfigLocation

11 classpath:applicationContext.xml

12

13

14

15 org.springframework.web.context.ContextLoaderListener16

17

18

19

20 encoding

21

22 org.springframework.web.filter.CharacterEncodingFilter23

24

25 encoding

26 UTF-8

27

28

29

30 encoding

31 /*

32

33

34

35 springmvc

36

37 org.springframework.web.servlet.DispatcherServlet38

39

40 contextConfigLocation

41 classpath:springmvc-config.xml

42

43

44 1

45

46

47 springmvc

48 /

49

50

整合应用测试

到这里利用 Maven 整合 SSM 框架的所有操作已经完成了,那么接下来让我们验证一下,刚刚的整合操作是否存在问题呢?

建立 Student.java 持久化类

用于映射数据库表的持久化类。

1 packagepo;2

3 public classStudent {4 privateInteger id;5 privateString stuno;6 privateString name;7 privateInteger age;8 publicInteger getId() {9 returnid;10 }11 public voidsetId(Integer id) {12 this.id =id;13 }14 publicString getStuno() {15 returnstuno;16 }17 public voidsetStuno(String stuno) {18 this.stuno =stuno;19 }20 publicString getName() {21 returnname;22 }23 public voidsetName(String name) {24 this.name =name;25 }26 publicInteger getAge() {27 returnage;28 }29 public voidsetAge(Integer age) {30 this.age =age;31 }32 @Override33 publicString toString() {34 return "Student [id=" + id + ", stuno=" + stuno + ", name=" + name + ", age=" + age + "]";35 }36 }

建立StudentDao接口文件和映射文件

接口文件中定义类一个根据 id 查询学生信息的方法。

映射文件中变细了对应这个方法的 sql 语句。

1 packagedao;2

3 importpo.Student;4

5 public interfaceStudentDao {6 publicStudent findStudentById(Integer id);7 }

1 <?xml version="1.0" encoding="UTF-8"?>

2 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

5

6

7 select * from t_stu_ex51 where id=#{id}8

9

建立 StudentService 接口文件

定义通过 id 查询学生信息的方法

1 packageservice;2

3 importpo.Student;4

5 public interfaceStudentService {6 publicStudent findStudentById(Integer id);7 }

建立 StudentServiceImpl.java 实现类

在类中调用了 CustomerDao 的查询学生信息的方法。

实现类采用了基于注解的形式, @Service 注解标识业务层的实现类, @Transactional 注解标识类中的所有方法都进行了 spring 中的事务管理, @Autowired 注解将 CustomerDao 接口对象注入到类中。

1 packageservice.impl;2

3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.stereotype.Service;5 importorg.springframework.transaction.annotation.Transactional;6

7 importdao.StudentDao;8 importpo.Student;9 importservice.StudentService;10

11 @Service12 @Transactional13 public class StudentServiceImpl implementsStudentService {14

15 @Autowired16 privateStudentDao studentDao;17

18 publicStudent findStudentById(Integer id) {19 return this.studentDao.findStudentById(id);20 }21 }

建立 StudentController.java 类

用于处理页面请求的控制器类 CustomerController,编写了一个根据 id 查询学生信息的方法,该方法将获取到的学生信息返回到了 student 视图中,即 student.jsp 页面中。

采用了基于注解的形式, @Controller 来标识控制器类, @Autowired 将 StudentService 接口对象注入到类中

1 packagecontroller;2

3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.stereotype.Controller;5 importorg.springframework.ui.Model;6 importorg.springframework.web.bind.annotation.RequestMapping;7

8 importpo.Student;9 importservice.StudentService;10

11 @Controller12 public classStudentController {13 @Autowired14 privateStudentService studentService;15 @RequestMapping("/findStudentById")16 publicString findStudentById(Integer id, Model model) {17 Student student =studentService.findStudentById(id);18 model.addAttribute("student", student);19 return "student";20 }21 }

建立 student.jsp 文件

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

学生信息

8

9

10

11

12

编号

13

学号

14

姓名

15

年龄

16

17

18

${student.id}

19

${student.stuno}

20

${student.name}

21

${student.age}

22

23

24

25

将项目发布到 tomcat 中

输入URL测试

9f98c6a9e68e560a27f07e07dbfcac02.png

写这篇博客主要是为了记录一下 Maven 整合框架的基本操作。实现的功能也都是最基本的功能。就当留个印象,以后再开发项目可以回过头来看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值