Sring MVC基于Java Config方式配置Mybatis, 无XM…

2020-04-13 16:01:12来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Sring MVC基于Java Config方式配置Mybatis, 无XML

初学Spring真是伤不起呀,连一个Mybatis的配置都整了一天才弄出来,太不容易了,所以这里一定要把它记录下来,防止自已到时候又忘记了,虽然前后经历了好长时间,但好在磕磕碰碰的弄出来了,也算给自已一点小安慰吧。

其实Mybatis的配置网上的资料真的不要太多,百度一下会找到好多,也许就是因为太多了,加上好多的文章都差不多,有时候想找点有用的东西还真不是那么容易的事。而且,由于Mybatis版本的原因,找到的内容好多已经过时了,所以中间经历的过程就不细说了,都是泪呀,当然可能也是因为我初学,菜鸟的原因吧,哎。

网上大部分能找到的 Mybatis的配置很多都是基于XML配置这种,那种也不是我想要的,我的整个Spring MVC工程也是用的Java config,就是没有在web.xml或者其它的配置文件去配置Bean这种,所以,我的Mybatis也想用Java代码的方式,这个就有一点不一样了,所以在配置中遇到了一些问题。

Mybatis相关网站

mybatis

mybatis-spring

像Maven、Spring MVC的配置过程这里就不说了,这不是本文的重点,所以Mybatis的配置是在Spring框架配置好的基础上进行的。

目录结构

在配置之前,先看下整个项目的目录结构

pom.xml的配置

需要在这个文件里面增加Mybatis、Mybatis-spring、MySql的一些依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>4.3.18.RELEASE</spring.version>
    <spring.jdbc.version>5.2.0.RELEASE</spring.jdbc.version>
    <mysql.connector.version>5.1.21</mysql.connector.version>
    <servlet.version>3.0.1</servlet.version>
    <jsp.version>2.2.1-b03</jsp.version>
    <jstl.version>1.2</jstl.version>
    <redis.version>2.0.3.RELEASE</redis.version>
    <jedis.version>2.9.0</jedis.version>
    <mybatis.version>3.5.4</mybatis.version>
    <mybatis.spring.version>2.0.4</mybatis.spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

      <dependency>
          <groupId>javax.servlet.jsp.jstl</groupId>
          <artifactId>jstl-api</artifactId>
          <version>1.2</version>
      </dependency>

    <!--  Servlet API 3.0  -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>${jsp.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <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>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</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>

  <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
  <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
      <version>3.0.11.RELEASE</version>
  </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.jdbc.version}</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.connector.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>${redis.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>${jedis.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.16</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis.spring.version}</version>
    </dependency>
  </dependencies>

再添加一个Mybatis的Bean配置,文件在 config/MyBatisConfig.java

package com.smartrui.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@PropertySource("classpath:config/application.properties")
@MapperScan(basePackages = {"com.smartrui.dao"})
public class MyBatisConfig {

    @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;

    @Value("${mybatis.type.alias.package}")
    private String mybatisTypeAliasPackage;

    @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;
    }

    @Bean
    @Autowired
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource  dataSource) throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource);

        sqlSessionFactoryBean.setTypeAliasesPackage(mybatisTypeAliasPackage);

        //指定mapper路径地址
        PathMatchingResourcePatternResolver classPathResource = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(classPathResource.getResources("classpath*:config/mappers/*.xml"));

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration() ;

        sqlSessionFactoryBean.setConfiguration(configuration);

        return sqlSessionFactoryBean;
    }


}

上面的 SqlSessionFactoryBean方法,主要有数据库的配置

sqlSessionFactoryBean.setDataSource(dataSource);和 mapper xml的路径配置 sqlSessionFactoryBean.setMapperLocations, 那个dataSource可以配置数据源像Mysql、Oracle和其它的连接池配置等。

文件加好后,需要在WebConfig.java这个文件中保证能扫描到,用 ComponentScanImport的方式弄进去

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.smartrui.dao","com.smartrui.service","com.smartrui.controller"})
@Import({DataBaseConfig.class,MyBatisConfig.class}) 
public class WebAppConfig extends WebMvcConfigurerAdapter {
    /**
     * 静态资源过滤
     */
    @Override
    public void addResourceHandlers( ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }
	....

注入SqlSessionFactory

新的MyBatis的sessionFactory加载有些不太一样,需要指定下,我开始一直报SqlSessionFactory不存在,后面搜索了下,我在dao下建立一个公共的commonDAO,加上如下内容就可以了, 注意有 @Autowired这个

package com.smartrui.dao;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

public class CommonDAO extends SqlSessionDaoSupport {

    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
}

好了,增加上面的几个文件后,Mybatis应该就算是配置好了,没弄好之前感觉好难,其实做好后,你会发现就是那么几个配置就行了,接下来,来一个简单的测试。

service下面写一个简单的查询

package com.smartrui.service;
import org.apache.ibatis.annotations.Param;
public interface PersonService {
   String getUserName(@Param("id") Integer id);
}

写一个service的实现

package com.smartrui.service.impl;

import com.smartrui.dao.PersonDAO;
import com.smartrui.dao.UserDAO;
import com.smartrui.model.User;
import com.smartrui.service.PersonService;
import com.smartrui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("PersonService")
public  class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDAO personDAO ;

    @Override
    public String getUserName(Integer id) {
        return personDAO.getUserName(id);
    }

}

dao层加上接口

package com.smartrui.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonDAO {

//    @Select("SELECT user_name FROM tb_user WHERE id = #{id}")
    String getUserName(@Param("id") Integer id);
}

config/mapper/PersonMapper.xml里面加上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="com.smartrui.dao.PersonDAO">
    <select id="getUserName" resultType="String">
        SELECT user_name FROM tb_user WHERE id = #{id}
    </select>
</mapper>

上面有几个注意的地方

  • dao层里面的方式如 getUserName要和PersonMapper.xml里面具体查询里的 id值一样。
  • PersonMapper.xmlnamespace的值,就是dao层那个包的路径。

上面的写完后,在Controller层增加一个service的调用就行了

 @RequestMapping("/getbyid")
    public String getById(){

        String name;
        try {
            name  = personService.getUserName(11);
        }catch(Exception e){
            e.printStackTrace();
            name  = "N/A";
        }
        System.out.println("name:" + name);
        return "user";
    }

通过上面的配置就能把Mybatis搭建起来了,至于更深层原理性的东西,这个我暂时没有时间去深究了,待后续框架用熟一些了再说吧,当前的目标就是能先用起来。


原文链接:https://www.cnblogs.com/smartrui/p/12694820.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:面试官:你和队友之间选一个淘汰,你怎么选?

下一篇:Oracle之序列