SpringData
对于数据访问层,无论是nosql还是sql数据库,SpringBoot的底层都是用Springdata去统一处理
SpringBoot:Springdata的官方文档地址:
https://spring.io/projects/spring-data
新项目:Spring-boot-data
项目搭建完成后肯定导入相关启动器,
之后是数据源的配置:
使用学习mybtis的数据库,配置相关yml
SpringBoot的默认是用的mysql8,所以如果出现时区报错,那么我们就添加时区,
serverTimezone="UTC"来解决异durid
druid
durid的常用配置参数
配置druid步骤
导入相关依赖,
配置对应的数据源
对应配置文件
spring: datasource: driver-class-name: com.mysql.jdbc.Driver password: xxx username: xxx url: jdbc:mysql://localhost:3306/mybatis #?serverTimezone=UTC&useUnicode=true&charcterEncoding=UTF-8 type: com.alibaba.druid.pool.DruidDataSource
去将我们的增删改查方法测试一下就好了,用的是jdbc模版,由boot提供:jdbcTemplate
package com.hyc.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class Datacontroller { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/userlist") public List<Map<String,Object>> userlist(){ String sql = "select * from mybatis.user"; List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); return maps; } @RequestMapping("/addUser") public String addUser(){ String sql = "insert into mybatis.user(id,name,pwd) values(1,'小胡','123456')"; jdbcTemplate.update(sql); return "insert"; } @RequestMapping("/upddateUser/{id}") public String upddateUser(@PathVariable("id") int id){ String sql = "update mybatis.user set name=?,pwd=? where id ="+id; Object[] obj = new Object[2]; obj[0] = "小明"; obj[1] = "66666"; jdbcTemplate.update(sql,obj); return "update"; } @RequestMapping("/DeleteUser/{id}") public String DeleteUser(@PathVariable("id") int id){ String sql = "delete from mybatis.user where id=? "; jdbcTemplate.update(sql,id); return "DeleteUser"; } }
YML配置druid常用的一些配置
#SpringBoot默认是不注入这些的,需要自己绑定 #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.Properity #则导入log4j 依赖就行 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
配置sql监控,和过滤请求:这里注意不要私自改动:loginusername等key
package com.hyc.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; import javax.servlet.ServletRegistration; import javax.sql.DataSource; import java.util.HashMap; @Configuration public class DruidConfing { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource(){ return new DruidDataSource(); } //后台监控 @Bean public ServletRegistrationBean ServletRegistrationBean(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); HashMap<String, String> InitParameters = new HashMap<>(); InitParameters.put("loginUsername","admin"); InitParameters.put("loginPassword","123456"); InitParameters.put("allow",""); //后台监控 bean.setInitParameters(InitParameters); return bean; } @Bean public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(); //可以过滤那些请求? bean.setFilter(new WebStatFilter()); bean.addUrlPatterns("/*"); bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return bean; } }
SpringBoot整合mybatis
准备工作,
寻找依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
去创建使用mybatis的需要文件,
实体类:user
mapper接口:usermapper
配置文件:usermapper.xml
创建完毕后我们去boot的配置文件配置mybatis:
#mybatis配置
mybatis.config-location=classpath:mapper/*.xml
mybatis.type-aliases-package=com.hyc.pojo
用mybatis的注解来 映射,实体类:
package com.hyc.mapper; import com.hyc.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface userMapper { List<User> getuser(); User getuserByid(int id); int addUser(User user); int UpdUser(User user); int DelUser(int id); }
之后编写映射文件:
<?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"> <!-- namespace 写入 自己 dao 里的接口名称--> <mapper namespace="com.hyc.mapper.userMapper"> <insert id="addUser"> insert into mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd}); </insert> <update id="UpdUser"> update mybatis.user set user.name = #{name},user.pwd =#{pwd} where id = #{id}; </update> <delete id="DelUser"> delete from mybatis.user where id=#{id} </delete> <select id="getuser" resultType="com.hyc.pojo.User"> select * from mybatis.user </select> <select id="getuserByid" resultType="com.hyc.pojo.User"> select * from mybatis.user where id=#{id}; </select> </mapper>
编写控制层测试:
@RestController public class usercontroller { @Autowired private userMapper userMapper; @RequestMapping("/queryList") public List<User> queryList(){ List<User> getuser = userMapper.getuser(); for (User user : getuser) { System.out.println(user); } return getuser; } }
之后去启动类上扫描mapper包的注解即可
@SpringBootApplication @MapperScan(value = "com.hyc.mapper") public class musicserver8888Main { public static void main(String[] args) { SpringApplication.run(musicserver8888Main.class,args); } }