1.在pom文件中导入相关的maven依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.19</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
2.在yml文件中配置数据库信息
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/user?serverTimezone=UTC # 我的时区一直出错,调整时区。 drive-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 利用type属性换成 Druid的数据源,毕竟阿里爸爸,还有监控什么的 # 下面的是配置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,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3.写DruidConfig让配置的Druid生效
@Configuration public class DruidConfig { //令yml文件中其他的配置生效 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置Druid的监控 //1、配置一个管理后台的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String,String> initParams = new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","");//默认就是允许所有访问 bean.setInitParameters(initParams); return bean; } //2、配置一个web监控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
4.在model下写相应的JavaBean
5.在mapper下写相应的操作数据库的Mapper文件
//指示这是一个操作数据库的 Mapper @Mapper public interface UserMapper { @Delete("delete from zuser where id=#{id}") public void DeleteUser(int id); @Select("select * from zuser where id=#{id}") public user FindUser(int id); @Insert("insert into zuser(id,username,password,type,money) values(#{id}, #{username},#{password},#{type},#{money})") public void AddUser(user user); @Update("update zuser set username=#{username},password=#{password}, type=#{type}, money=#{money} where id=#{id}") public void UpdateUser(user user); }
6.写相应的Controller
@RestController public class UserController { @Autowired JdbcTemplate jdbcTemplate; @Autowired UserMapper u; //利用Spring Boot中的JdbcTempalte来操作数据库 @RequestMapping("/mapping") public List<Map<String, Object>> getuser(){ List<Map<String, Object>> list=jdbcTemplate.queryForList("select * from zuser"); return list; } //利用@PathVariable来获取输入的id值返回相应的user对象 @RequestMapping("/getuserbyid/{id}") public user getuserbyid(@PathVariable("id") int id){ return u.FindUser(id); } @RequestMapping("/deleteuserbyid/{id}") public void deleteuserbyid(@PathVariable("id") int id){ u.DeleteUser(id); } @RequestMapping("/insertuser") public user insertuser(user uu){ u.AddUser(uu); return uu; } @RequestMapping("/updateuser") public user updateuser(user uu){ u.UpdateUser(uu); return uu; } }
7.运行程序测试