java config mybatis_Sring MVC基于Java Config方式配置Mybatis, 无XML

  • Post author:
  • Post category:java


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

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

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

Mybatis相关网站

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

目录结构

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

47fc153f02830cc910d924fe2e6ee88b.png

pom.xml的配置

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

UTF-8

1.7

1.7

4.3.18.RELEASE

5.2.0.RELEASE

5.1.21

3.0.1

2.2.1-b03

1.2

2.0.3.RELEASE

2.9.0

3.5.4

2.0.4

junit

junit

4.11

test

javax.servlet.jsp.jstl

jstl-api

1.2

javax.servlet

javax.servlet-api

${servlet.version}

javax.servlet.jsp

jsp-api

${jsp.version}

provided

javax.servlet

jstl

${jstl.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-test

${spring.version}

test

org.springframework

spring-context

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.thymeleaf

thymeleaf-spring4

3.0.11.RELEASE

org.springframework

spring-jdbc

${spring.jdbc.version}

mysql

mysql-connector-java

${mysql.connector.version}

org.springframework.data

spring-data-redis

${redis.version}

redis.clients

jedis

${jedis.version}

junit

junit

4.12

test

org.springframework

spring-test

${spring.version}

test

org.mybatis

mybatis

${mybatis.version}

com.alibaba

druid

1.0.16

org.mybatis

mybatis-spring

${mybatis.spring.version}

再添加一个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这个文件中保证能扫描到,用 ComponentScan或Import的方式弄进去

@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的配置

/p>

PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”

“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>

SELECT user_name FROM tb_user WHERE id = #{id}

上面有几个注意的地方

dao层里面的方式如 getUserName要和PersonMapper.xml里面具体查询里的 id值一样。

PersonMapper.xml的namespace的值,就是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搭建起来了,至于更深层原理性的东西,这个我暂时没有时间去深究了,待后续框架用熟一些了再说吧,当前的目标就是能先用起来。



版权声明:本文为weixin_42380646原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。