当前位置:   article > 正文

java ssm项目_SSM项目整合基本步骤

java ssm项目整合histryx

SSM项目整合

1、基本概念

1.1、Spring

Spring 是一个开源框架, Spring 是于 2003  年兴起的一个轻量级的 Java  开发框架,由 Rod Johnson  在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。 Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而, Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。 简单来说, Spring 是一个轻量级的控制反转( IoC )和面向切面( AOP )的容器框架。

1.2、SpringMVC

Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面。 Spring MVC  分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

1.3、MyBatis

MyBatis  本是 Apache 的一个开源项目iBatis, 2010 年这个项目由 apache software foundation  迁移到了 google code ,并且改名为 MyBatis  。 MyBatis 是一个基于 Java 的 持久层框架。 iBATIS 提供的持久层框架包括 SQL Maps 和 Data Access Objects ( DAO ) MyBatis  消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。 MyBatis  使用简单的  XML 或注解用于配置和原始映射,将接口和  Java  的 POJOs ( Plain Old Java Objects ,普通的  Java 对象)映射成数据库中的记录。

2、SSM 项目整合——系统结构图

116316531_1_20171116094924578.png

3、SSM项目结构搭建

3.1、需要的Jar包

①Spring:

spring-aop-4.3.0.RELEASE.jar

spring-aspects-4.3.0.RELEASE.jar

spring-beans-4.3.0.RELEASE.jar

spring-context-4.3.0.RELEASE.jar

spring-context-support-4.3.0.RELEASE.jar

spring-core-4.3.0.RELEASE.jar

spring-expression-4.3.0.RELEASE.jar

spring-jdbc-4.3.0.RELEASE.jar

spring-tx-4.3.0.RELEASE.jar

spring-web-4.3.0.RELEASE.jar

spring-webmvc-4.3.0.RELEASE.jar

spring-webmvc-portlet-4.3.0.RELEASE.jar

spring-websocket-4.3.0.RELEASE.jar

com.springsource.net.sf.cglib-2.2.0.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.2.jar

③MyBatis:

mybatis-3.1.1.jar

log4j-1.2.16.jar

mybatis-spring-1.2.1.jar

④文件上传/下载:

commons-fileupload-1.2.2.jar

commons-io-2.4.jar

⑤C3P0数据源:

c3p0-0.9.5.1.jar

mchange-commons-java-0.2.10.jar

classmate-0.8.0.jar

⑥MySQL:

mysql-connector-java-5.0.8-bin.jar

⑦JSTL:

taglibs-standard-compat-1.2.5.jar

taglibs-standard-impl-1.2.5.jar

taglibs-standard-jstlel-1.2.5.jar

taglibs-standard-spec-1.2.5.jar

⑧JSON:

jackson-annotations-2.6.0.jar

jackson-core-2.6.0.jar

jackson-databind-2.6.0.jar

⑨Hibernate Validate:

hibernate-validator-4.3.0.Final.jar

validation-api-1.0.0.GA.jar

validation-api-1.1.0.CR1.jar

jboss-logging-3.1.0.CR2.jar

jboss-logging-3.1.1.GA.jar

3.2、SSM项目的目录结构

116316531_2_20171116094924813.png

3.3、web.xml

116316531_3_20171116094924953.gif

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

2

3 xmlns="http://xmlns.jcp.org/xml/ns/javaee"

4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

5 id="WebApp_ID"version="3.1">

6 SSM_01

7

8 index.jsp

9

10

11

12

13

14

15 contextConfigLocation

16 classpath:spring/applicationContext.xml

17

18

19

20

21 org.springframework.web.context.ContextLoaderListener

22

23

24

25

26

27

28 springDispatcherServlet

29 org.springframework.web.servlet.DispatcherServlet

30

31

32 contextConfigLocation

33 classpath:springmvc/springMvc.xml

34

35 1

36

37

38

39

40

41 springDispatcherServlet

42

43 /

44

45

46

47

48

49

50 encodingFilter

51 org.springframework.web.filter.CharacterEncodingFilter

52 true

53

54 encoding

55 UTF-8

56

57

58

59 encodingFilter

60 /*

61

62

63

64

65

66

67 HiddenHttpMethodFilter

68 org.springframework.web.filter.HiddenHttpMethodFilter

69

70

71

72

73

74 HiddenHttpMethodFilter

75 /*

76

77

116316531_3_20171116094924953.gif

3.4、applicationContext.xml

116316531_3_20171116094924953.gif

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

2

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

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

5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

8

9

10

11 expression="org.springframework.stereotype.Component" />

12

13 expression="org.springframework.stereotype.Repository" />

14

15 expression="org.springframework.stereotype.Service" />

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

116316531_3_20171116094924953.gif

3.5、springMvc.xml

116316531_3_20171116094924953.gif

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

2

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

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

5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

9

10

11

12

13

14 expression="org.springframework.stereotype.Controller" />

15

16

17

18

19 class="org.springframework.web.servlet.view.InternalResourceViewResolver">

20

21

22

23

24

25

26

27

28

29

30

31

32

116316531_3_20171116094924953.gif

3.6、mybatis-config.xml

116316531_3_20171116094924953.gif

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

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

5

6

7

8

9

116316531_3_20171116094924953.gif

3.7、db.properties

1 user=root

2 password=root

3 driverClass=com.mysql.jdbc.Driver

4 jdbcUrl=jdbc:mysql://localhost:3306/ssm_01

3.8、log4j.properties

116316531_3_20171116094924953.gif

1 # Global logging configuration

2 log4j.rootLogger=DEBUG, stdout

3 # Console output...

4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender

5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

7

8 log4j.logger.java.sql.ResultSet=INFO

9 log4j.logger.org.apache=INFO

10 log4j.logger.java.sql.Connection=DEBUG

11 log4j.logger.java.sql.Statement=DEBUG

12 log4j.logger.java.sql.PreparedStatement=DEBUG

116316531_3_20171116094924953.gif

3.9、实体类:User.java

116316531_3_20171116094924953.gif

1 package cn.com.zfc.ssm.entity;

2

3 import org.springframework.stereotype.Component;

4

5 /**

6 * 定义实体类:如果实体类的属性名和数据表中的字段名不一致,则需要在 映射文件中配置 resultMap7 *8 *@authorFuchangZhang9 *10 */

11 @Component

12 public class User {

13   private Integer id;

14   private String userName;

15   private String userSex;

16   private Integer userAge;

17

18   public User() {

19     super();

20   }

21

22   public User(String userName, String userSex, Integer userAge) {

23     super();

24     this.userName = userName;

25     this.userSex = userSex;

26     this.userAge = userAge;

27   }

28

29   public User(Integer id, String userName, String userSex, Integer userAge) {

30     super();

31     this.id = id;

32     this.userName = userName;

33     this.userSex = userSex;

34     this.userAge = userAge;

35   }

36

37   public Integer getId() {

38     return id;

39   }

40

41   public void setId(Integer id) {

42     this.id = id;

43   }

44

45   public String getUserName() {

46     return userName;

47   }

48

49   public void setUserName(String userName) {

50     this.userName = userName;

51   }

52

53   public String getUserSex() {

54     return userSex;

55   }

56

57   public void setUserSex(String userSex) {

58     this.userSex = userSex;

59   }

60

61   public Integer getUserAge() {

62     return userAge;

63   }

64

65   public void setUserAge(Integer userAge) {

66     this.userAge = userAge;

67   }

68

69   @Override

70   public String toString() {

71     return "User [id=" + id + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge + "]";

72   }

73

74 }

116316531_3_20171116094924953.gif

3.10 UserMapper.java

116316531_3_20171116094924953.gif

1 package cn.com.zfc.ssm.dao;

2

3 import java.util.List;

4

5 import org.springframework.stereotype.Repository;

6

7 import cn.com.zfc.ssm.entity.User;

8

9 /**

10 * 定义接口:方法名得和相应的 mapper.xml 中的 id 保持一致11 *12 *@authorFuchangZhang13 *14 */

15 @Repository

16 public interface UserMapper {

17

18   //添加一条用户信息

19   public int save(User user);

20

21   //删除一条用户信息

22   public int delete(int id);

23

24   //修改一条用户信息

25   public int update(User user);

26

27   //查询一条用户信息

28   public User getUser(int id);

29

30   //查询全部用户信息

31   public List getUsers();

32

33 }

116316531_3_20171116094924953.gif

3.11 UserMapper.xml

116316531_3_20171116094924953.gif

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23 insert into user_info

24 (user_name,user_sex,user_age)

25 values

26 (#{userName},#{userSex},#{userAge})

27

28

29

30 delete from user_info

31 where

32 id=#{id}

33

34

35

36

37 update user_info

38 set

39 user_name=#{userName},user_sex=#{userSex},user_age=#{userAge}

40 where

41 id=#{id}

42

43

44

45

46   select * from

47   user_info

48   where

49   id=#{id}

50

51

52

53

54 select * from user_info

55

56

116316531_3_20171116094924953.gif

3.12、UserService.java

116316531_3_20171116094924953.gif

1 package cn.com.zfc.ssm.service;

2

3 import java.util.List;

4

5 import org.springframework.stereotype.Repository;

6

7 import cn.com.zfc.ssm.entity.User;

8

9 @Repository

10 public interface UserService {

11   //添加一条用户信息

12   public boolean add(User user);

13

14   //删除一条用户信息

15   public boolean delete(int id);

16

17   //修改一条用户信息

18   public boolean update(User user);

19

20   //查询一条用户信息

21   public User queryOne(int id);

22

23   //查询全部用户信息

24   public List queryAll();

25 }

116316531_3_20171116094924953.gif

3.13、UserServiceImpl.java

116316531_3_20171116094924953.gif

1 package cn.com.zfc.ssm.service.impl;

2

3 import java.util.List;

4

5 import org.springframework.beans.factory.annotation.Autowired;

6 import org.springframework.stereotype.Service;

7

8 import cn.com.zfc.ssm.dao.UserMapper;

9 import cn.com.zfc.ssm.entity.User;

10 import cn.com.zfc.ssm.service.UserService;

11

12 @Service("userService")

13 public class UserServiceImpl implements UserService {

14

15   @Autowired

16   private UserMapper userMapper;

17   private int result;

18

19   @Override

20   public boolean add(User user) {

21     result = userMapper.save(user);

22     if (result > 0) {

23       return true;

24     } else {

25       return false;

26     }

27   }

28

29   @Override

30   public boolean delete(int id) {

31     result = userMapper.delete(id);

32     if (result > 0) {

33       return true;

34     } else {

35       return false;

36     }

37   }

38

39   @Override

40   public boolean update(User user) {

41     result = userMapper.update(user);

42     if (result > 0) {

43       return true;

44     } else {

45       return false;

46     }

47   }

48

49   @Override

50   public User queryOne(int id) {

51     User user = userMapper.getUser(id);

52     return user;

53   }

54

55   @Override

56   public List queryAll() {

57     List users = userMapper.getUsers();

58     return users;

59   }

60

61 }

116316531_3_20171116094924953.gif

3.14、UserHandler.java

116316531_3_20171116094924953.gif

1 package cn.com.zfc.ssm.handler;

2

3 import java.util.Map;

4

5 import org.springframework.beans.factory.annotation.Autowired;

6 import org.springframework.stereotype.Controller;

7 import org.springframework.web.bind.annotation.RequestMapping;

8

9 import cn.com.zfc.ssm.entity.User;

10 import cn.com.zfc.ssm.service.UserService;

11

12 @Controller

13 public class UserHandler {

14

15 @Autowired

16 private UserService userService;

17

18   //添加页面

19   @RequestMapping("/addView")

20   public String addView(Map map) {

21     map.put("user", new User());

22     return "addUser";

23   }

24

25   //保存操作

26   @RequestMapping("/addUser")

27   public String addUser(User user) {

28     if (userService.add(user)) {

29       return "success";

30     } else {

31       return "redirect:/queryAll";

32     }

33   }

34

35   //删除操作

36   @RequestMapping("/delete")

37   public String delete(int id) {

38     if (userService.delete(id)) {

39       System.out.println("删除成功");

40     } else {

41       System.out.println("删除失败");

42     }

43     return "redirect:/queryAll";

44   }

45

46   //查询一个操作

47   @RequestMapping("/queryOne")

48   public String queryOne(int id, Map map) {

49     map.put("user", userService.queryOne(id));

50     return "update";

51   }

52

53   //修改操作

54   @RequestMapping("/update")

55   public String update(User user, Map map) {

56     if (userService.update(user)) {

57       System.out.println("修改成功");

58       return "redirect:/queryAll";

59     } else {

60       System.out.println("修改失败");

61       map.put("user", userService.queryOne(user.getId()));

62       return "update";

63     }

64   }

65

66   //查询全部操作

67   @RequestMapping("/queryAll")

68   public String queryAll(Map map) {

69     map.put("users", userService.queryAll());

70     return "queryAll";

71   }

72 }

116316531_3_20171116094924953.gif

3.15、index.jsp

116316531_3_20171116094924953.gif

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

Insert title here

8

9

10 添加用户

11


12 查询全部

13

14

116316531_3_20171116094924953.gif

3.16、addUser.jsp

116316531_3_20171116094924953.gif

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

8

9

10

用户添加

11

12

13

用户添加

14

15

16 UserName:

17


18

19 Mapsexes= newHashMap<>();20 sexes.put("男","男");21 sexes.put("女","女");22 request.setAttribute("sexes", sexes);23 %>

24 Sex:

25

26


27 UserAge:

28


29

30

31

32

116316531_3_20171116094924953.gif

3.17、success.jsp

116316531_3_20171116094924953.gif

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

Insert title here

8

9

10

Success Page!!!

11

12

116316531_3_20171116094924953.gif

3.18、queryAll.jsp

116316531_3_20171116094924953.gif

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

8

9

Insert title here

10

11 table, tr, th, td{

12 border:1px solid black;

13 }

14

15 table{

16 width:80%;

17 }

18

19

20

21

22

23

24

ID

25

UserName

26

UserSex

27

UserAge

28

Operator

29

30

31

32

33 0 }">

34

35

36

${user.id }

37

${user.userName }

38

${user.userSex }

39

${user.userAge }

40

Delete

42 Update

44

45

46

47

48 没有人

49

50

51

52

53

54

116316531_3_20171116094924953.gif

3.19、update.jsp

116316531_3_20171116094924953.gif

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

8

9

10

Insert title here

11

12

13

用户修改

14

15 method="post"modelAttribute="user">

16

17

18 UserName:

19


20

21 Mapsexes= newHashMap<>();22 sexes.put("男","男");23 sexes.put("女","女");24 request.setAttribute("sexes", sexes);25 %>

26 Sex:

27

28


29 UserAge:

30


31

32

33

34

116316531_3_20171116094924953.gif

四、数据库ssm_01

数据表:user_info

id、user_name、user_sex、user_age

注:由于本人技术有限,此文章仅供参考

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/200097
推荐阅读
相关标签
  

闽ICP备14008679号