SpringBoot整合数据库
必须导入的依赖:
<!--mysql驱动--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.32</version> </dependency>
如果用mybatis的话就用
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>
如果是自己实现的话就用jdbc的依赖
配置数据库的信息
spring.datasource.username=root spring.datasource.password=root spring.datasource.url= jdbc:mysql://localhost:3306/boot spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
将上述的配置整合后SpringBoot就会自动生成DataSource类 ,就可以直接用
springBoot集成jdbc的所有属性方法(源码中JdbcTemplate类)
public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { private static final String RETURN_RESULT_SET_PREFIX = "#result-set-"; private static final String RETURN_UPDATE_COUNT_PREFIX = "#update-count-"; private boolean ignoreWarnings = true; private int fetchSize = -1; private int maxRows = -1; private int queryTimeout = -1; private boolean skipResultsProcessing = false; private boolean skipUndeclaredResults = false; private boolean resultsMapCaseInsensitive = false; public JdbcTemplate() { }
相关功能就可以通过jdbcTemplate实现
通过原生的JDBC查询数据
@RestController public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/test01") public String testJDBC(){ String sql = "select * from employee"; List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); for (Map map : maps){ System.out.println(map); } return "maps"; } }
配置信息
在springBoot的application.properties文件中
mybatis.mapper-locations=classpath:com.demo.mapper/*.xml mybatis.type-aliases-package=com.demo.pojo # 开启下划线与驼峰命名的转换 mybatis.configuration.map-underscore-to-camel-case=true
SpringBoot整合MyBatis
连接信息及其相关依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--mysql驱动--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.32</version> </dependency>
逆向工程驱动
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
逆向工程配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- targetRuntime: 执行生成的逆向工程的版本 MyBatis3Simple: 生成基本的CRUD(简洁版) MyBatis3: 生成带条件的CRUD(终极版) --> <!--数据库本地jar包--> <classPathEntry location="D:\lib\mysql-connector-java-8.0.29.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3Simple"> <property name="javaFileEncoding" value="UTF-8"/> <!--suppressAllComments 设置为true 则不再生成注释--> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库的连接信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/boot" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- javaBean : 数据库表对应的实体类--> <javaModelGenerator targetPackage="com.demo.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- SQL映射文件 : 与Mapper接口对应 --> <sqlMapGenerator targetPackage="com.demo.mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- Mapper接口 :实现相关功能的接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 逆向工程的表 与 需要创建的类名 --> <!-- tableName 设置为*号,可以对应所有表,此时可以不写domainObjectName --> <!-- domainObjectName属性指定 生成出来的实体类的类名 --> <table tableName="employee" domainObjectName="employee"/> </context> </generatorConfiguration>
通过generator:generate即可生成