欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring整合c3p0整合mybatis。spring使用java配置类来整合配置c3p0数据源和mybatis。

程序员文章站 2022-05-24 15:08:13
...

本文采取java配置类来配置spring,整合了c3p0连接池和Mybatis,只需要以下6个部分即可配置完成。

注:dao接口、mapper映射文件、实体类、数据库的库和表没有在此给出。

1. maven配置

	<properties>
		<java.version>1.8</java.version>
		<spring.version>4.3.18.RELEASE</spring.version>
		<cglib.version>2.2.2</cglib.version>
	</properties>

	<dependencies>
		<!-- Spring core & mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>5.1.38</version>
		</dependency>
				

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<type>jar</type>
			<scope>test</scope>
		</dependency>

		<!-- CGLib for @Configuration -->
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib-nodep</artifactId>
			<version>${cglib.version}</version>
			<scope>runtime</scope>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.4.6</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>1.3.2</version>
		</dependency>
		
	<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
	<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
	<dependency>
	    <groupId>com.mchange</groupId>
	    <artifactId>c3p0</artifactId>
	    <version>0.9.5.2</version>
	</dependency>

2. spring配置类


@Configuration
@ComponentScan(basePackages= {"hzz.try1"})
@Import({JDBCConfig.class})
public class RootConfig {
	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource((javax.sql.DataSource) dataSource);
		return sqlSessionFactoryBean.getObject();
	}
	
	//扫描找到mapper.xml文件
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer(SqlSessionFactory sqlSessionFactory) {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
		mapperScannerConfigurer.setBasePackage("hzz.try1.mybatis.mapper");
		return mapperScannerConfigurer;
	}
}

3. spring配置类需要Import的JDBCConfig配置类


@PropertySource("classpath:db.properties")
public class JDBCConfig implements EnvironmentAware{

    Environment env;
    
    //注入spring的environment从而获得properties内的数据
	@Override
	public void setEnvironment(Environment environment) {
		this.env=environment;
	}
    /**
     * 创建c3p0数据源对象
     * @return
     * @throws PropertyVetoException 
     */
    @Bean
    public DataSource dataSource() throws PropertyVetoException{
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass(env.getProperty("jdbc.driver"));;
        comboPooledDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
        comboPooledDataSource.setUser(env.getProperty("jdbc.username"));
        comboPooledDataSource.setPassword(env.getProperty("jdbc.password"));
        return comboPooledDataSource;
    }

}

4. 连接数据库的配置文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/try1?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=root

5. mybatis的Mapper.xml、Mapper接口、实体类、mysql数据库的库和表

因人而异,不详细写了。

6. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RootConfig.class)
public class DAOTest {
	@Autowired
	UserMapper userMapper;

	@Test
	public void selectUserByIdTest() {
		User user = userMapper.findUserById(0);
		System.out.println(user);
	}
}