spring、springmvc和mybatis整合(java config方式)

  之前项目中使用ssm框架大多是基于xml的方式,spring3.0以后就提供java config的模式来构建项目,并且也推荐使用这种方式,自从接触过springboot后,深深感受到这种纯java配置的便利,但是springboot默认为我们引入好多jar和配置,使得项目变得很重,因此决定自己动手搭建一个无xml的ssm项目。

  开发环境

  我的开发环境如下:

  web服务器:tomcat8

  开发工具:STS

  JDK版本:1.8

  项目构建工具:maven

  搭建过程

  1、首先引入相关依赖

  pom文件如下:

<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>io.powerx</groupId>
    <artifactId>springmvcconfig</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
    </properties>
    <dependencies>
        <!-- spring相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- springmvc相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

        <!-- servlet配置,不配置也不影响,但是jsp会报错 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- 加载jackson包 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!-- druid数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.16</version>
        </dependency>
        <!-- spring和mybatis整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- MySQL数据库连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>
        <!-- MyBatis框架 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- 添加logback日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 2、编写配置文件

  MyWebAppInitializer继承AbstractAnnotationConfigDispatcherServletInitializer,并重写其中的方法(另外一种方式是MyWebAppInitializer直接实现WebApplicationInitializer接口,不过会复杂一点),它是我们程序的入口,web容器启动后会调其中相关的方法从而启动整个应用。主要实现3个方法:getRootConfigClasses,负责加载spring容器,本例中我们只加载了RootConfig类,其它一些spring相关配置类也可以在这里加载;getServletConfigClasses,加载springmvc容器,本例中我们加载了MvcConfig;getServletMappings,设置映射的路径。配置代码如下:

  MyWebAppInitializer.java

package powerx.io.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{MvcConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

  RootConfig.java

package powerx.io.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableTransactionManagement //开启事务支持
@Import(DruidDataSourceConfig.class)//导入数据源的配置
@ComponentScan(basePackages = {"powerx.io.service","powerx.io.dao","powerx.io.config"},
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
public class RootConfig {
}

  MvcConfig.java

package powerx.io.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("powerx.io.controller")
public class MvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        super.addViewControllers(registry);
    }

    //配置jsp视图
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    //配置静态资源处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//将静态资源的请求转发到servlet容器中默认的servlet上
    }
}

  DruidDataSourceConfig.java

package powerx.io.config;

import java.io.IOException;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan(basePackages="powerx.io.dao")
public class DruidDataSourceConfig{

    @Value("${spring.datasource.url}")
    private String dbUrl;  

    @Value("${spring.datasource.username}")
    private String username;  

    @Value("${spring.datasource.password}")
    private String password;  

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;  

    @Value("${spring.datasource.initialSize}")
    private int initialSize;  

    @Value("${spring.datasource.minIdle}")
    private int minIdle;  

    @Value("${spring.datasource.maxActive}")
    private int maxActive;  

    @Value("${spring.datasource.maxWait}")
    private int maxWait;  

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;  

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;  

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;  

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;  

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;  

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;  

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;  

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;  

    @Value("${spring.datasource.filters}")
    private String filters;  

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;  

    @Bean     //声明其为Bean实例
    public DataSource dataSource(){
        DruidDataSource datasource = new DruidDataSource();  

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
        }
        datasource.setConnectionProperties(connectionProperties);
        return datasource;
    }

    //mybatis的配置
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException{
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//mybatis-plus插件类
        sqlSessionFactoryBean.setDataSource(dataSource());//数据源
        sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath:mappers/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("powerx.io.model");//别名,让*Mpper.xml实体类映射可以不加上具体包名
        return sqlSessionFactoryBean;
    }
}

  3、配置数据源和mapper映射文件等

  jdbc.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testssm
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

  UserMapper.xml

<?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">
<mapper namespace="powerx.io.dao.UserDao">
    <resultMap type="User" id="UserResult">
        <!-- 数据库字段和实体类属性映射关系,名称一样的字段可以无需映射。column对应数据库的实际字段,property对应实体类的属性 -->
        <result column="USER_NAME" property="userName" />
        <result column="USER_PWD" property="userPwd" />
    </resultMap>

    <select id="findByName" parameterType="String" resultMap="UserResult">
        SELECT USER_NAME,USER_PWD FROM USER WHERE USER_NAME=#{userName}
    </select>

    <insert id="insertUser" parameterType="powerx.io.model.User">
    INSERT INTO USER(USER_NAME,USER_PWD) VALUES(#{userName},#{userPwd})
  </insert>

</mapper>

  User.java

package powerx.io.model;

public class User {

    private String userName;
    private String userPwd;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPwd() {
        return userPwd;
    }
    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

}

  UserDao.java 

package powerx.io.dao;

import powerx.io.model.User;

public interface UserDao {

    User findByName(String userName);

    int insertUser(User user);
}

4、控制层和service层

  UserService.java

package powerx.io.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import powerx.io.dao.UserDao;
import powerx.io.model.User;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    public Object findByName(String userName) {
        return userDao.findByName(userName);
     }

    @Transactional
    public int insertUser(User user) {
        //测试事务是否起作用
        userDao.insertUser(user);
        return userDao.insertUser(user);
     }

}

  UserContoller.java

package powerx.io.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import powerx.io.model.User;
import powerx.io.service.UserService;

@Controller
public class UserContoller {

    public static Logger logger = LoggerFactory.getLogger("monitor");
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/get")
    public Object getData(String userName) {
        logger.info("获取用户信息");
        return userService.findByName(userName);
    }

    @ResponseBody
    @RequestMapping("/insert")
    public Object insert(User user) {
        logger.info("添加用户");
        return userService.insertUser(user);
    }
}

5、项目结构图

  

  总结

  写完代码,在往tomcat中部署运行的时候遇到了一个问题,tomcat启动失败,报了一大堆异常,其中一个是java.util.zip.ZipException: invalid LOC header (bad signature),从网上找了一大堆答案,基本肯定是依赖的jar包不完整造成的,但是我搜索了一下maven仓库,删除了所有in_progress的包,依然不行,无奈只能一个一个的排除,浪费了好久时间,最后查出原因是因为mysql的包没有下载完整,但是依然是一个.jar结尾的文件,只不过大小不对,所以在此记录一下,以后在遇到tomcat启动失败问题,一定要细心排查所依赖的jar。

  至此,整个ssm框架搭建完毕,在其中我加入了事务控制和日志管理,基本上可以作为一个生产项目的基础demo。项目代码基本上都以贴上,我也上传到了码云上面,方便自己使用,地址:https://gitee.com/hehang_com/ssm.git

  

  

原文地址:https://www.cnblogs.com/hhhshct/p/9688079.html

时间: 2024-07-31 17:37:20

spring、springmvc和mybatis整合(java config方式)的相关文章

spring,springMvc和mybatis整合配置

一,配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/n

springmvc与mybatis整合时 java.lang.IllegalArgumentException: Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required 异常

今天在整合springmvc与mybatis时,启动服务器遇到这样一个问题, by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.util.Assert.notNull(Assert.java:112) 异常的意思是  缺少sqlSessionFactory 或者是  sqlSessionTe

SpringMVC与mybatis整合

一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"   "http://mybatis.org/dtd/mybatis-generator-config_1_0.d

框架 day68 SpringMVC入门(框架原理,springmvc和mybatis整合)

第一天:注解开发基础(springmvc入门) springmvc框架原理(掌握) DispatcherServlet前端控制器.HandlerMapping处理器映射器.HandlerAdapter处理器适配器.ViewResolver视图解析器 springmvc入门程序 目标:加深对springmvc三大组件的认识 查询商品信息 重点掌握注解的HandlerMapping处理器映射器.HandlerAdapter处理器适配器(掌握) springmvc和mybatis整合(掌握) 通过一个

SpringMvc基础知识(二) springmvc和mybatis整合

1 springmvc和mybatis整合 1.1 需求 使用springmvc和mybatis完成商品列表查询. 1.2 整合思路 springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在spring中进行注册. 第二步:整合service层 通过spring管理 service接口. 使用配置方式将service接口配置在spring配置文件中. 实现事务

SpringMVC与MyBatis整合之日期格式转换

在上一篇博客<SpringMVC与MyBatis整合(一)——查询人员列表>中遗留了日期格式转换的问题,在这篇记录解决过程. 对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定.         将请求日期数据串传成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致. 在上一篇的示例程序中,Person类属性如下: 而测试结果如下: 所以自定义参数绑定将日期串转成java.util.Date类型.需要向处理器适配器中注入自定义的参数绑定组件. 添加

SSM(MyBatis+Spring+SpringMVC)之MyBatis总结

对于SSM(MyBatis+Spring+SpringMVC)之MyBatis总结 对于ORM持久化框架之前一直是用的JDBC去连接数据库 ,对于JDBC来连接库来说可能存在一些不足,那么MyBatis确切的说只能算半持久化框架,因为MyBatis是需要我们去自动的编写我们的SQL语句的,我们可以用JDBC&MyBatis做一些比较 我们在使用JDBC的时候会对数据库进行一些频繁创建连接和释放连接的操作从而影响的整个系统的性能.那么针对这一方面我们的MyBatis很好的利用了数据库连接池来对我们

SpringMVC+FreeMarker+Mybatis 整合

这个项目自己有时写写测试代码什么的, 代码比较简单,代码已放在 github : https://github.com/zhouyongtao/homeinns-web 目录结构: 贴一下Maven的POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&quo

整合spring,springmvc和mybatis

我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <