增加 spring mybatis 样例

This commit is contained in:
罗祥
2018-12-21 17:55:46 +08:00
parent 964df531a8
commit 6cd6826468
29 changed files with 796 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
package com.heibaiying.bean;
import lombok.Data;
/**
* @author : heibaiying
* @description :
*/
@Data
public class Relation {
private String id;
private String name;
}

View File

@ -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;
}

View File

@ -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[]{"/"};
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);
}