springBoot笔记

读取yaml的配置

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

验证的配置

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

thymeleaf

	  xmlns:th="http://www.thymeleaf.org"
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

国际化

MyLocalResolver类
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
    String language= httpServletRequest.getParameter("l");
    Locale locale=Locale.getDefault();

    if(!StringUtils.isEmpty(language)){
        String[] split= language.split("_");
     locale= new Locale(split[0],split[1]);
    }
    return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}

}
国际化注入
@Bean
public LocaleResolver localeResolver(){
return new MyLocalResolver();

}
MyMvcConfig类
@Bean
public LocaleResolver localeResolver(){
    return new MyLocalResolver();

}
拦截器
**LoginHandlerInterceptor类**
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    Object loginUser=request.getSession().getAttribute("loginUser");
    if(loginUser==null){
        request.setAttribute("msg","没有权限,请登陆");
       request.getRequestDispatcher("/index.html").forward(request,response);
       return false;
    }else {
    return true;
    }
}
}

登录控制器

public class LoginController {
@GetMapping("/user/login")
public String login(String username, String password,
                    Model model, HttpSession session){
    if(!StringUtils.isEmpty(username)&&"123456".equals(password)){
        session.setAttribute("LoginUser",username);

        return "redirect:/main.html";
    }else {
        model.addAttribute("msg","用户名或者密码错误");
        return "index";
    }
}
}
数据库连接

ymal文件

		spring:
		  datasource:
		    username: root
		    password: 19990207
		    url: jdbc:mysql://localhost:3306/mybatis?ccuseUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
		    driver-class-name: com.mysql.cj.jdbc.Driver

依赖

	  <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>
    <dependency>

CRUD

@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/userList")
public List<Map<String,Object>> userList(){
    String sql="select * from account";
    List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
    return maps;
}
@GetMapping("/addUser")
public String addUser(){
    String sql="insert into mybatis.account(id,name,password) value (3,'junyu','123')";
    jdbcTemplate.update(sql);
    return "update-ok";
}

@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id){
    String sql="update mybatis.account set name=?,password=? where id="+id;

    Object[] objects = new Object[2];
    objects[0]="junyu";
    objects[1]="111";
    jdbcTemplate.update(sql,objects);
    return "update-ok";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id){
    String sql="delete from mybatis.account where id=?";
    jdbcTemplate.update(sql,id);
    return "update-ok";
}
}
Druid

依赖

        <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>0.2.23</version>
    </dependency>

yaml文件

	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

druidConfig类

@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource DruidDatasource() {
    return new DruidDataSource();
}
@Bean
public ServletRegistrationBean StatViewServlet() {
    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;
}

}

Mybatis依赖
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
yaml文件
mybatis.type-aliases-package=com.junyu.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
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.junyu.mapper.UserMapper">

<select id="queryUserList" resultType="User">
    select * from account
</select>

<select id="queryUserById" resultType="User">
    select *from account where id=#{id}
</select>

<insert id="addUser" parameterType="User">
    insert into account (id,name,password) values (#{id},#{name},#{password})
</insert>

<update id="updateUser" parameterType="User">
    update account set name=#{name},password=#{password} where id=#{id}
</update>
controller类
@RestController
public class UserController {

@Autowired
private UserMapper userMapper;

@GetMapping("/queryUserList")
public List<User> queryUserList(){
    List<User> users = userMapper.queryUserList();
    for(User user:users){
        System.out.println(user);
    }
    return users;
}
@GetMapping("/queryUserById")
public User queryUserById(){
    User users = userMapper.queryUserById(1);
    return users;
}

@GetMapping("/addUser")
public String addUser(){
    User user=new User(4,"enen","11");
    userMapper.addUser(user);
    return"ok";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值