使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)

网友投稿 254 2023-01-07


使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)

(项目结构图参照文章末尾)

1、创建maven工程,在pom.xml文件中导入需要的jar包依赖:

4.0.0

com.test

ssm

0.0.1-SNAPSHOT

war

4.12

4.1.3.RELEASE

3.2.8

1.2.2

1.2.15

5.1.32

1.6.4

2.4.2

1.0.9

4.3.5

1.2

2.5

2.0

2.5

3.3.2

1.3.2

3.3

3.4.2-fix

1.3.1

2.7.2

4.10.3

2.4.0

joda-time

joda-time

${joda-time.version}

org.apache.commons

commons-lang3

${commons-lang3.version}

org.apache.commons

commons-io

${commons-io.version}

commons-net

commons-net

${commons-net.version}

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

org.apache.httpcomponents

httpclient

${httpclient.version}

junit

junit

${junit.version}

test

org.slf4j

slf4j-log4j12

${slf4j.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

${mybatis.spring.version}

com.github.miemiedev

mybatis-paginator</artifactId>

${mybatis.paginator.version}

com.github.pagehelper

pagehelper

${pagehelper.version}

mysql

mysql-connector-java

${mysql.version}

com.alibaba

druid

${druid.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-aspects

${spring.version}

jstl

jstl

${jstl.version}

javax.servlet

servlet-api

${servlet-api.version}

provided

javax.servlet

jsp-api

${jsp-api.version}

provided

commons-fileupload

commons-fileupload

${commons-fileupload.version}

redis.clients

jedis

${jedis.version}

org.apache.solr

solr-solrj

${solrj.version}

io.springfox

springfox-swagger2

${swagger.version}

io.springfox

springfox-swagger-ui

${swagger.version}

    

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

org.apache.maven.plugins

maven-compiler-plugin

3.2

1.7

1.7

UTF-8

2、整合SpringMVC:

(1)在web.xml文件中配置前端控制器:

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

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

id="serviceMarket" version="2.5">

ssm-test

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

contextConfigLocation

classpath:spring/applicationContext*.xml

org.springframework.web.context.ContextLoaderListener

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

ssm-test

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

1

ssm-test

/

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

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

id="serviceMarket" version="2.5">

ssm-test

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

contextConfigLocation

classpath:spring/applicationContext*.xml

org.springframework.web.context.ContextLoaderListener

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

ssm-test

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

1

ssm-test

/

(2)编写springmvc.xml文件:

在文件中配置与springmvc框架相关的配置,比如:springmvc.xml约束、处理器映射器、处理器适配器、视图解析器、Handler等配置。

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

    

    

         

        

       

    

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

    

    

         

        

       

    

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

(3)编写Handler:

//User类的Handler

//需要在类上面加@Controller注解,这样才能被自动扫描到

@Controller

@RequestMapping("/user")

public class UserController {

private static Logger log=LoggerFactory.getLogger(UserController.class);

@RequestMapping(value="/login",method=RequestMethod.GET)

public String login(HttpServletRequest request,Model model){

String username = request.getParameter("username");

String password = request.getParameter("password");

System.out.println("username:"+username+";password:"+password);

User user=null;

if (username.equals("zwp") && password.equals("123456")) {

user = new User();

user.setAge(11);

user.setId(1);

user.setPassword("123456");

user.setUsername("zwp");

log.debug(user.toString());

}

model.addAttribute("user", user);

return "index";

}

}

//User实体类

public class User {

private Integer id;

private String username;

private String password;

private Integer age;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUsername() {

return uhttp://sername;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

@Override

public String toString() {

return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";

}

}

(4)配置日志文件:

在项目根目录下面创建log4j.properties日志文件,下面是日志文件的基本配置,可以满足大多数项目的需求。

log4j.rootLogger=INFO,Console,File

#定义日志输出目的地为控制台

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

#可以灵活地指定日志输出格式,下面一行是指定具体的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

#指定输出目录

log4j.appender.File.File = logs/ssm.log

#定义文件最大大小

log4j.appender.File.MaxFileSize = 10MB

# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志

log4j.appender.File.Threshold = ALL

log4j.appender.File.layout = org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

(5)启动项目,进行测试:

访问地址:http://localhost:8080/ssm/user/login?username=zwp&password=123456,如果出现下面页面则代表映射成功。

至此,SpringMVC的配置就已经成功。

3、Spring和Mybatis框架的整合:

整合思路:由Spring通过单例方式管理SqlSessionFactory;spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession,持久层的mapper都需要有spring进行管理。

(1)配置dao层,即配置sqlSessionFactory:(我的配置在applicationContext-dao.xml文件中)

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

destroy-method="close">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

destroy-method="close">

destroy-method="close">

destroy-method="close">

创建并编写SqlMapConfig.xml文件:

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

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

(2)配置service层:(我的配置在applicationContext-service.xml文件中)

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

(3)配置事务:(我的配置在applicationContext-trans.xml文件中)

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

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

pointcut="execution(* com.zwp.service.*.*(..))" />

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

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

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

pointcut="execution(* com.zwp.service.*.*(..))" />

pointcut="execution(* com.zwp.service.*.*(..))" />

至此,Spring和Mybatis框架的整合就完成了。

4、测试:

(1)创建测试用表:

(2)利用MyBatis-Generator自动创建实体类、Mybatis映射文件以及Dao接口。

(3)建立Service和实现类:

//UserService接口类:

@Service

public interface UserService {

public User getUserById(int userId);

}

//UserService的实现类

@Service

public class UserServiceImpl implements UserService {

//注入UserMapper接口

@Autowired

private UserMapper userMapper;

@Override

public User getUserById(int userId) {

User user = userMapper.selectByPrimaryKey(userId);

return user;

}

}

(3)建立UserController类:

//User类的Handler

//需要在类上面加@Controller注解,这样才能被自动扫描到

@Controller

@RequestMapping("/user")

public class UserController {

@Autowired

private UserService userService;

private static Logger log=LoggerFactory.getLogger(UserController.class);

@RequestMapping(value="/getUserById/{userId}",method=RequestMethod.GET)

public @ResponseBody User getUserById(@PathVariable Integer userId){

return userService.getUserById(userId);

}

@RequestMapping(value="/login",method=RequestMethod.GET)

public String login(HttpServletRequest request,Model model){

String username = request.getParameter("username");

String password = request.getParameter("password");

System.out.println("username:"+username+";password:"+password);

User user=null;

if (username.equals("zwp") && password.equals("123456")) {

user = new User();

user.setAge(11);

user.setId(1);

user.setPassword("123456");

user.setUsername("zwp");

log.debug(user.toString());

}

model.addAttribute("user", user);

return "index";

}

}

(4)部署项目,输入地址:http://localhost:8080/ssm/user/getUserById/2,出现下面响应结果,则表示整合成功了。

至此,Maven+Spring+SpringMVC+Mybatis的整合就完成了。

附:项目结构图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关怎么调用服务(微服务网关实现的功能)
下一篇:Django之多对多查询与操作方法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~