本文采取java配置类来配置spring,整合了c3p0连接池和Mybatis,只需要以下6个部分即可配置完成。
注:dao接口、mapper映射文件、实体类、数据库的库和表没有在此给出。
0. 项目路径
src/main/java–hzz–try1–mybatis–mapper–Mapper.xml、Mapper接口
src/main/java–hzz–try1–configuration–RootConfig.java
src/main/java–hzz–try1–domain–实体类
src/test/java–test–DAOTest.java
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-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"})
public class RootConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource((javax.sql.DataSource) dataSource);
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//开启驼峰命名映射
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;
}
//配置数据源
@Bean
public DataSource dataSource() throws PropertyVetoException{
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/try1?useUnicode=true&characterEncoding=utf8&useSSL=false");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("root");
return comboPooledDataSource;
}
}
3. mybatis的Mapper.xml、Mapper接口、实体类、mysql数据库的库和表
因人而异,不详细写了。
4. 测试
@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);
}
}