SpringBoot操作数据库
springboot整合JDBC
整合谁就需要在pom.xml引入它的starts,如果整合JDBC需要引入这些
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
偏向yaml配置数据源
spring:
datasource:
username: root
password: 密码
url: jdbc:mysql://你的地址/数据库
driver-class-name: com.mysql.cj.jdbc.Driver
然后自动装配JdbcTemplate,使用Datasource也可以
@Autowired
JdbcTemplate jdbcTemplate;;
完成这些我们就可以愉快的操作了,我们来测试一下
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JdbcController {
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping("/jdbc")
public List<Map<String, Object>> userList(){
String sql="select * from mall_users";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
}
springboot配置阿里的driud数据源
首先引入对应的jar
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
配置application.yaml
spring:
datasource:
username: root
password: jx922289
url: jdbc:mysql://101.34.181.173:3306/mall
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
max-active: 100
min-idle: 1
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-on-return: false
test-on-borrow: true
test-while-idle: true
filters: stat,wall,log4j
connection-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
stat-view-servlet:
url-pattern: /druid/*
reset-enable: true
login-username: admin
login-password: admin
enabled: true
use-global-data-source-stat: true
springboot配置mybatis
首先先引入mybatis的start,数据源配置和druid相同
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
如果需要配置自己需要的属性的话在application.yaml中进行配置即可,开头是mybatis
引入了start就可以进行操作了,操作如下
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
@Select("select * from mall_users")
List<User> findUserList();
}
比如我们要进行操作就可以在Dao层的接口上加上@Mapper,在使用注解写sql语句就可以进行操作了,加上了@Mapper当扫描到这个注解的时候就会通过动态代理的方式生成实现类,并注入到ioc容器中,这样的话我们就不需要写dao层的的实现类了。
同样我们也可以通过配置文件的方式进行写sql语句
我们进行同样的操作采用配置文件的方式
编写user-mapper.xml
<?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="com.xu.demo.dao.UserMapper">
<select id="findUserList" resultType="com.xu.demo.pojo.User">
select * from mall_users
</select>
</mapper>
这里注意由于User类和配置文件不在一个包下resultType需要写完整路径
接着我们在application.yaml配置路径
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
这样的话完成以上配置就完成了对吗mybatis进行整合
|