赞
踩
**
**
1.创建一个springboot项目,请选择以下的依赖
2.项目创建成功,配置yml文件(不要忘记设置编码跟时区)
#配置用户名跟密码等需要属性
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
3.在test中测试,查看数据库是否连接成功
@SpringBootTest class SpringbootDataApplicationTests { @Autowired DataSource dataSource; //注入数据源 @Test void contextLoads() throws SQLException { //查看默认数据源:class com.zaxxer.hikari.HikariDataSource:dpcp,c3po System.out.println(dataSource.getClass()); //获得数据库连接 Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }
4.数据库连接成功,创建controller可以对数据库进行最基本的增删改查操作
@RestController public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; //查询数据库的所有信息 //没有实体类,数据库中的东西,怎么获取?map @RequestMapping("/uesrList") public List<Map<String, Object>> uesrList() { String sql = "select * from tbuser"; List<Map<String, Object>> list_map = jdbcTemplate.queryForList(sql); return list_map; } @RequestMapping("addUser") public String addUser() { String sql = "insert into mybatis.tbuser (name,pwd,teaId) values ('小明','234',1)"; jdbcTemplate.update(sql); return "ok"; } @RequestMapping("updateUser/{id}/{pwd}") public String updateUser(@PathVariable("id") int id, @PathVariable("pwd") String pwd) { String sql = "update mybatis.tbuser set pwd =? where id= ?"; Object[] object = new Object[2]; object[0] = pwd; object[1] = id; jdbcTemplate.update(sql, object); return "ok"; } }
**
**
1.加上druid跟log4j依赖包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.继续配置yml文件(一定注意格式问题:尤其是有无空格)
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3.编写配置类
@Configuration public class DruidConfig { //把yml的数据文件跟配置类进行绑定,这样我们就可以用yml中的配置了 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource() { return new DruidDataSource(); } @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");//访问页面 Map<String, String> initParameters = new HashMap<>(); //initParameters put 的所有key 都是固定的人家写好的 initParameters.put("loginUsername", "admin"); //loginUsername 键是固定的 initParameters.put("loginPassword", "123");//loginPassword key 键是固定的 //允许谁可以访问 initParameters.put("allow", "");//默认允许所有 bean.setInitParameters(initParameters);//设置初始化参数 return bean; } }
4.测试
在地址栏上访问,对数据进行增删改查,在登录http://localhost:8080/druid/即可查看监
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。