SpringBoot如何整合数据库,实现CURD操作
一、整合原生JDBC 1.1 导入相关依赖 1 2 3 4 5 6 7 8 9 10 11 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jdbc</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency >
1.2 配置文件连接数据库 1 2 3 4 5 6 spring: datasource: username: root password: 123 url: jdbc:mysql://localhost:3306/jdbctest?serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver
1.3 CURD测试 在Controller层实现相关业务,数据库操作方法全部封装在jdbcTemplate类中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 @Controller public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/select") @ResponseBody public List<Map<String, Object>> selectPerson(){ String sql = "select * from person" ; List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql); return mapList; } @RequestMapping("/update") @ResponseBody public String addPerson () { String sql = "insert into person value('盐取', 80, 45787)" ; jdbcTemplate.update(sql); return "add-ok" ; } @RequestMapping("/delete/{name}") @ResponseBody public String deletePerson (@PathVariable("name") String name) { String sql = "delete from person where name = ?" ; jdbcTemplate.update(sql, name); return "delete OK!" ; } @RequestMapping("/add/{name}/{age}/{passowrd}") @ResponseBody public String addPerson (@PathVariable("name") String name, @PathVariable("age") Integer age, @PathVariable("password") String password) { String sql = "update `person` value(?, ?, ?)" ; jdbcTemplate.update(sql, name, age, password); return "update-ok" ; } }
二、整合Mybatis 2.1 导入相关依赖 1 2 3 4 5 6 7 8 9 10 11 12 <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.1.3</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency >
2.2 配置文件连接数据库 1 2 3 4 5 6 7 8 9 10 11 12 13 spring: datasource: username: root password: 123 url: jdbc:mysql://localhost:3306/jdbctest?serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mybatis/mapper/*.xml
2.3 Mybatis基础操作
其他操作和使用Mybtias差异不大,只是Mapper.xml文件,放进resources/mapper目录下
1 2 3 4 5 6 7 8 9 JavaBean @Data @AllArgsConstructor @NoArgsConstructor public class Person { private String name; private Integer age; private String password; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Mapper接口 @Mapper @Repository public interface PersonMapper { List<Person> queryPersons () ; Person queryPersonByName (String name) ; int addPerson (Person person) ; int deletePersonByName (String name) ; int updatePerson (Person person) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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.letere.mapper.PersonMapper" > <select id ="queryPersons" resultType ="com.letere.bean.Person" > select * from `person` </select > <select id ="queryPersonByName" resultType ="com.letere.bean.Person" > select * from `person` where name like #{name} </select > <insert id ="addPerson" > insert into `person` value (#{name}, #{age}, #{password}) </insert > <delete id ="deletePersonByName" > delet from `person` where name = #{name} </delete > <update id ="updatePerson" > update `person` set age = #{age}, password = #{password} where name = #{name} </update > </mapper >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Service层实现类 @Service public class PersonService implements PersonMapper { @Autowired private PersonMapper mapper; @Override public List<Person> queryPersons () { return mapper.queryPersons(); } @Override public Person queryPersonByName (String name) { name = '%' + name + '%' ; return mapper.queryPersonByName(name); } @Override public int addPerson (Person person) { return mapper.addPerson(person); } @Override public int deletePersonByName (String name) { return mapper.deletePersonByName(name); } @Override public int updatePerson (Person person) { return mapper.updatePerson(person); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Controller层调用Service层 @RestController public class PersonController { @Autowired private PersonService server; @RequestMapping("/person/queryAll") public List<Person> queryAll (Model model) { List<Person> persons = server.queryPersons(); return persons; } @RequestMapping("/person/queryByName/{name}") public Person queryByName (@PathVariable("name") String name) { Person person = server.queryPersonByName(name); return person; } }
三、整合druid数据连接池 3.1 导入相关依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 <dependency > <groupId > com.alibaba</groupId > <artifactId > druid-spring-boot-starter</artifactId > <version > 1.2.1</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > 1.2.17</version > </dependency >
3.2 配置文件设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 spring: datasource: username: root password: 123 url: jdbc:mysql://localhost:3306/jdbctest?serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: filters: stat,wall,log4j max-pool-prepared-statement-per-connection-size: 20 use-global-data-source-stat: true connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillus=500 initial-size: 3 min-idle: 3 max-active: 10 max-wait: 60000
3.3 配置druid后台监控
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource () { return new DruidDataSource(); } @Bean public ServletRegistrationBean servletRegistrationBean () { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*" ); HashMap<String, String> initParameters = new HashMap<>(); initParameters.put("loginUsername" , "莱特雷" ); initParameters.put("loginPassword" , "123" ); initParameters.put("allow" , "" ); initParameters.put("用户名" , "ip地址" ); bean.setInitParameters(initParameters); return bean; } @Bean public FilterRegistrationBean filterRegistrationBean () { FilterRegistrationBean<Filter> filterBean = new FilterRegistrationBean<>(); filterBean.setFilter(new WebStatFilter()); HashMap<String, String> initParameters = new HashMap<>(); initParameters.put("exclusions" , "*.js, *.css, /druid/*" ); filterBean.setInitParameters(initParameters); return filterBean; } }