1. 程式人生 > >Spring-Javaconfig-Mybatis-JNDI連線mysql配置

Spring-Javaconfig-Mybatis-JNDI連線mysql配置

本文主要演示通過jndi配置資料來源,通過spring-javaconfig的方式配置mybatis,連線mysql資料來源

主要看程式碼

1,maven工程中新增依賴

<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.30</version>
		</dependency>
2,DataSourceConfig

package com.halfworlders.idat.config;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class DataSourceConfig extends WebMvcConfigurerAdapter {

	@Bean
	public JndiObjectFactoryBean dataSource() {
		JndiObjectFactoryBean jndiObjectFB = new JndiObjectFactoryBean();
		jndiObjectFB.setJndiName("jndi/mybatis");
		jndiObjectFB.setResourceRef(true);
		jndiObjectFB.setProxyInterface(javax.sql.DataSource.class);
		return jndiObjectFB;
	}

	
	@Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return sessionFactory;
    }
	
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer(){
		MapperScannerConfigurer mScannerConfigurer = new MapperScannerConfigurer();
		mScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
		mScannerConfigurer.setBasePackage("com.halfworlders.idat.dao");
		return mScannerConfigurer;
	}
	

	@Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
3,Tomcat中配置jndi/mybatis

在\apache-tomcat-7.0.79\conf\context.xml裡面新增配置如下

<?xml version='1.0' encoding='utf-8'?>
<Context>
	<Resource name="jndi/mybatis"
	auth="Container"
	type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	url="jdbc:mysql://****/**db"
	username="****"
	password="****"
	maxActive="20"
	maxIdle="10"
	maxWait="10000"/> 
</Context>
apache-tomcat-7.0.79\lib目錄新增 mysql-connector-java-5.1.30.jar檔案

4,com.halfworlders.idat.dao包內建立UserMapper介面

package com.halfworlders.idat.dao;

import com.halfworlders.idat.domain.User;

public interface UserMapper {
    User selectByPrimaryKey(String userId);
}

5,classpath:mapper目錄裡建立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="com.halfworlders.idat.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.halfworlders.idat.domain.User" >
    <id column="user_id" property="userId" jdbcType="CHAR" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="user_birthday" property="userBirthday" jdbcType="DATE" />
    <result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
  </resultMap>
  <sql id="Base_Column_List" >
    user_id, user_name, user_birthday, user_salary
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from t_user
    where user_id = #{userId,jdbcType=CHAR}
  </select>
</mapper>
以上步驟即是實現了mybatis訪問mysql資料