增加 spring mybatis 样例
This commit is contained in:
95
spring/spring-mybatis-annotation/pom.xml
Normal file
95
spring/spring-mybatis-annotation/pom.xml
Normal file
@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
<spring-base-version>5.1.3.RELEASE</spring-base-version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!--jdbc 相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<!--单元测试相关依赖包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-base-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3.0</version>
|
||||
</dependency>
|
||||
<!--mybatis 依赖包-->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class Flow {
|
||||
|
||||
private long id;
|
||||
|
||||
private long flowId;
|
||||
|
||||
private long plugId;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
public class Relation {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mysql.properties")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class DataSourceConfig {
|
||||
|
||||
/**
|
||||
* 感觉这种注入的方式并不够好
|
||||
* 没有spring-boot中使用@ConfigurationProperties(prefix = "config")指定前缀注入的方式优雅
|
||||
*/
|
||||
@Value("${mysql.driverClassName}")
|
||||
private String driverClassName;
|
||||
@Value("${mysql.url}")
|
||||
private String url;
|
||||
@Value("${mysql.username}")
|
||||
private String username;
|
||||
@Value("${mysql.password}")
|
||||
private String password;
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[0];
|
||||
}
|
||||
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class[]{ServletConfig.class};
|
||||
}
|
||||
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/"};
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement // 开启声明式事务处理 等价于xml中<tx:annotation-driven/>
|
||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||
public class ServletConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private DataSourceConfig sourceConfig;
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
*/
|
||||
@Bean
|
||||
public DriverManagerDataSource dataSource() {
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
dataSource.setUrl("jdbc:mysql://localhost:3306/mysql");
|
||||
dataSource.setUsername("root");
|
||||
dataSource.setPassword("root");
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置mybatis 会话工厂
|
||||
*
|
||||
* @param dataSource 这个参数的名称需要保持和上面方法名一致 才能自动注入,因为
|
||||
* 采用@Bean注解生成的bean 默认采用方法名为名称,当然也可以在使用@Bean时指定name属性
|
||||
*/
|
||||
@Bean
|
||||
public SqlSessionFactoryBean sessionFactoryBean(DriverManagerDataSource dataSource) throws IOException {
|
||||
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
|
||||
sessionFactoryBean.setDataSource(dataSource);
|
||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
sessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mappers/**/*.xml"));
|
||||
sessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatisConfig.xml"));
|
||||
return sessionFactoryBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置mybatis 会话工厂
|
||||
*/
|
||||
@Bean
|
||||
public MapperScannerConfigurer MapperScannerConfigurer() {
|
||||
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
|
||||
configurer.setSqlSessionFactoryBeanName("sessionFactoryBean");
|
||||
configurer.setBasePackage("com.heibaiying.dao");
|
||||
return configurer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义事务管理器
|
||||
*/
|
||||
@Bean
|
||||
public DataSourceTransactionManager transactionManager() {
|
||||
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
||||
manager.setDataSource(dataSource());
|
||||
return manager;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@Mapper
|
||||
public interface MysqlDao {
|
||||
|
||||
List<Relation> queryById(String id);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Mapper
|
||||
public interface OracleDao {
|
||||
|
||||
List<Flow> queryById(long id);
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.heibaiying.dao.OracleDao">
|
||||
|
||||
<select id="queryById" resultType="com.heibaiying.bean.Flow">
|
||||
select * from APEX_030200.WWV_FLOW_CALS where ID = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,14 @@
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.heibaiying.dao.MysqlDao">
|
||||
|
||||
|
||||
<select id="queryById" resultType="com.heibaiying.bean.Relation">
|
||||
SELECT help_keyword_id AS id,name
|
||||
FROM HELP_KEYWORD
|
||||
WHERE HELP_KEYWORD_ID = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
|
||||
<!-- mybatis 配置文件 -->
|
||||
<configuration>
|
||||
<settings>
|
||||
<!-- 开启驼峰命名 -->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
<!-- 打印查询sql -->
|
||||
<setting name="logImpl" value="STDOUT_LOGGING"/>
|
||||
</settings>
|
||||
|
||||
</configuration>
|
||||
|
||||
<!--更多settings配置项可以参考官方文档: <a href="http://www.mybatis.org/mybatis-3/zh/configuration.html"/>-->
|
@ -0,0 +1,5 @@
|
||||
# mysql <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
mysql.driverClassName=com.mysql.jdbc.Driver
|
||||
mysql.url=jdbc:mysql://localhost:3306/mysql
|
||||
mysql.username=root
|
||||
mysql.password=root
|
@ -0,0 +1,5 @@
|
||||
# oracle <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
oracle.driverClassName=oracle.jdbc.driver.OracleDriver
|
||||
oracle.url=jdbc:oracle:thin:@//IP<49><50>ַ:<3A>˿ں<CBBF>/<2F><><EFBFBD>ݿ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
|
||||
oracle.username=<EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>
|
||||
oracle.password=<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
@ -0,0 +1,36 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Relation;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
public class MysqlDaoTest {
|
||||
|
||||
@Autowired
|
||||
private MysqlDao mysqlDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Relation> relations = mysqlDao.queryById("691");
|
||||
if (relations != null) {
|
||||
for (Relation relation : relations) {
|
||||
System.out.println(relation.getId() + " " + relation.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.heibaiying.dao;
|
||||
|
||||
import com.heibaiying.bean.Flow;
|
||||
import com.heibaiying.config.DataSourceConfig;
|
||||
import com.heibaiying.config.DispatcherServletInitializer;
|
||||
import com.heibaiying.config.ServletConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {DispatcherServletInitializer.class, ServletConfig.class})
|
||||
public class OracleDaoTest {
|
||||
|
||||
@Autowired
|
||||
private OracleDao oracleDao;
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
List<Flow> flows = oracleDao.queryById(217584603977429772L);
|
||||
if (flows != null) {
|
||||
for (Flow flow : flows) {
|
||||
System.out.println(flow.getId() + " " + flow.getPlugId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user