持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情
1.聊聊如何写一个网站
1.1 有一套自己熟悉的后台模板 x-admin
X-admin基于layui的轻量级前端后台管理框架,简单免费,兼容性好,面向所有层次的前后端程序。
1.2 至少通过前端框架,自己可以组合出来一个前端页面
- Layui
- bootstap
- semantic-ui
- bootstap可视化布局系统(可以自行拼接组件)
- element-ui
Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。在日常开发中无论是小的组件还是标记,我都是使用bootstrap中的相关代码直接复制即可.
注意:我们在使用过程中一般不要修改相关网站的代码,都是直接给我们配置好的以防出现错误不容易查找.尤其是class或者id中的值.
2. JDBC
在加载JDBC的时候一般是通过java的反射机制来实现的.这是java的反射机制的应用场景
JDBC是Java语言中用来规范客户端程序如何来访问数据库的,提供了诸如查询和更新数据库中数据的方法。我们在使用的时候按照如下顺序.
源码分析
2.1 解决datasource
2.2 jdbcTemplate的使用
增删改查操作
package com.whx.springbootstudydata.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Objects;
//@restController相当于@responsebody+@controller返回字符串
@RestController
public class jdbcController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/userList")
public List<Map<String,Object>> userList(){
// 因为现在没有编写实体类所以可以通过map获取数据库中的东西
// 查询数据库中的信息
String sql="select * from user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@GetMapping("/addUser")
public String addUser(){
String sql = "insert into mybatis.user (id,name,pwd) values(8,'张久','55555555')";
jdbcTemplate.update(sql);
return "插入成功";
}
@GetMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") int id){
String sql="delete from mybatis.user where id=?";
jdbcTemplate.update(sql,id);
return "删除成功";
}
@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id){
String sql="update mybatis.user set name=?,pwd=? where id="+id;
Object[] objects = new Object[2];
objects[0]= "王八蛋";
objects[1]= "66666666";
jdbcTemplate.update(sql,objects);
return "更新成功";
}
}
3.整合Druid数据源
Druid 不是 Spring Boot 内部提供的技术,它属于第三方技术,我们可以通过以下两种方式进行整合:
- 自定义整合 Druid
- 通过 starter 整合 Druid
yaml配置文件整合
spring:
datasource:
username: root
password: 123456
# 加入时区报错了,就增加一个时区的配置就OK了serverTimezone=UTC
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
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
配置文件嵌入以后即可以使用
package com.whx.springbootstudydata.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.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource() {
return new DruidDataSource();
}
// 后台监控:web.xml, ServletRegistrationBean
// 因为SpringBoot 内置了 servlet容器,所以没有web.xml,替代方法ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//后台需要有人登陆,账号密码
Map<String, String> initParameters = new HashMap<>();
//增加配置
initParameters.put("loginUsername", "root");//登陆key 是固定的loginUsername loginPassword
initParameters.put("loginPassword", "admin");
// 允许谁可以访问
initParameters.put("allow", "");
//禁止谁能访问
//initParameters.put("kuangshen","192.168.1.2");
bean.setInitParameters(initParameters);//设置初始化参数
return bean;
}
// filter过滤器
@Bean
public FilterRegistrationBean webStatilter() {
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
bean.setFilter(new WebStatFilter());
//可以过滤那些请求
Map<String, String> initParameters = new HashMap<>();
//这些东西不进行统计
initParameters.put("exclusions", "*.js,*.cs,/druid/*");
bean.setInitParameters(initParameters);
return bean;
}
}
然后通过访问相关url获取在druid中的记录