標籤:manage list schema 取數 支援 blog ons mod 串連池
springBoot整合MyBatise及簡單應用
我採用的是 工具IDEA 架構是springBoot+maven+Mybatise
第一步:
pom.xml 引入相關jar包
<?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>cn.xudy.base</groupId> <artifactId>testMybase</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version> <mysql-connector.version>5.1.39</mysql-connector.version> </properties> <dependencies> <!-- MYSQL --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring Boot JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot.version}</version> </dependency> </dependencies> <!--maven打包時應用--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
第二部:
建立兩個設定檔類
1、 我採用了c3p0 JDBC 串連池這個檔案主要是配置mybatis 的xml設定檔位置
@Configuration//加上這個註解,使得支援事務@EnableTransactionManagementpublic class MyBatisConfig implements TransactionManagementConfigurer { @Bean(name = "dataSource") @Qualifier(value = "dataSource") @Primary @ConfigurationProperties(prefix = "c3p0") public DataSource dataSource() { return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build(); } @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource()); } @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource()); bean.setTypeAliasesPackage("cn.group.xudy.model");//每一張表對應的實體類 //添加XML目錄 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath:mapper/*Mapper.xml"));//每張表對應的xml檔案 return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } /** * 跨域 * @return */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }; }}
2、配置mapper java檔案
/** * 掃描mybatis的介面 *MyBatis掃描介面,使用的tk.mybatis.spring.mapper.MapperScannerConfigurer,如果你不使用通用Mapper,可以改為org.xxx... * @author zxj * */@Configuration// 因為這個對象的掃描,需要在MyBatisConfig的後面注入,所以加上下面的註解@AutoConfigureAfter(MyBatisConfig.class)public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); //擷取之前注入的beanName為sqlSessionFactory的對象 mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); //每張表對應的XXMapper.java interface類型的Java檔案 mapperScannerConfigurer.setBasePackage("cn.group.xudy.mapper");// Properties properties = new Properties();// properties.setProperty("mappers", "tk.mybatis.springboot.util.MyMapper");// properties.setProperty("notEmpty", "false");// properties.setProperty("IDENTITY", "MYSQL");// mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }}
注意 mybatis 的mapper.xml 在IDEA中一定要寫到resources下面
第三步 :
1.sql以註解的形式訪問資料庫擷取資料
在java下mapper檔案下一個下一個簡單的註解代碼測試下 下面是三種模式
@Select("select * from claims;") List<Map<String,Object>> find();@Select("Select * from claims where id = ‘${id}‘")Map<String,Object> selectClaimsName(@Param("id") String id);@Select("Select * from claims where id = #{id}")Map<String,Object> selectClaimsName(@Param("id") String id);
寫一個調用類根據自己的資料庫配置 應該沒什麼問題了
2、sql語句以xml形式也就是大家常用的mybatis的形式擷取資料
在java下mapper中寫調用方法
List<ClaimsModel> getAll();
在resources下xml寫到 我寫的是一對多的形式 有點亂 ,這部分就不細說了,如果不懂可以看看相關Mybatis的使用這裡只介紹整合
<?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="cn.group.xudy.mapper.claimsMapper"> <resultMap id="BaseResultMap" type="cn.group.xudy.model.ClaimsModel" > <id column="id" property="id" jdbcType="BIGINT" /> <id column="reason" property="reason" jdbcType="VARCHAR" /> <id column="files" property="files" jdbcType="VARCHAR" /> <!-- ofType指定students集合中的物件類型 --> <!--<association property="claimFilesModelList" javaType="cn.group.xudy.model.ClaimFilesModel">--> <!--</association>--> <collection property="claimFilesModelList" ofType="cn.group.xudy.model.ClaimFilesModel" column="id" select="getStudent"></collection> <!--<collection property="claimFilesModelList" ofType="cn.group.xudy.model.ClaimFilesModel" column="id">--> <!--<id column="file_name" property="fileName" jdbcType="VARCHAR" />--> <!--</collection>--> </resultMap> <sql id="Base_Column_List" > id,reason,files </sql> <select id="getAll" resultMap="BaseResultMap">-- select * from claims c,claim_files f WHERE c.id=f.claim_id select * from claims c,claim_files </select> <select id="getStudent" parameterType="int" resultType="cn.group.xudy.model.ClaimFilesModel"> SELECT * FROM claim_files WHERE claim_id=#{id} </select></mapper>
OK 代碼附上
https://gitee.com/xdymemory00/springbootAndMybatiseZhengHe.git
springBoot整合MyBatise及簡單應用