本篇博客采用的是springboot框架,持久层整合了mybatis来操作数据库,适合新手学习springboot整合mybatis
1.pom.xml: 引入其他需要的资源依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.8version>
dependency>
<dependency>
<groupId>org.webjarsgroupId>
<artifactId>jqueryartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.webjarsgroupId>
<artifactId>bootstrapartifactId>
<version>4.5.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
- 我配置的是Druid数据源,可以自行添加,用默认的数据源也可
- jquery 、bootstrap webjars 引入,前端框架引用
- 引入thymeleaf 模板引擎
2.application.yml: 配置文件
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql:///simpledemo?characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#数据源其他配置
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,slf4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
# 指定全局配置文件位置
config-location: classpath:mybatis/mybatisConfig.xml
# 指定sql映射文件位置
mapper-locations: classpath:mybatis/mapper/*.xml
#控制台打印sql语句
logging:
level:
com.baidu.springboot_10_simpledemo.mapper: debug
- 数据源其他配置可不写,需要配置数据源和整合mybatis需要的文件位置
3.templates/index.html、success.html: 登录页面和成功页面
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstraptitle>
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.5.0/css/bootstrap.css}" rel="stylesheet">
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
head>
<body class="text-center">
<form class="form-signin" th:action="@{/login}" action="dashboard.html" method="get">
<img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please sign inh1>
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">p>
<label class="sr-only">Usernamelabel>
<input type="text" class="form-control" name="username" placeholder="Username" required="" autofocus="">
<label class="sr-only">Passwordlabel>
<input type="password" class="form-control" name="password" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
label>
div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign inbutton>
<p class="mt-5 mb-3 text-muted">© 2017-2018p>
<a class="btn btn-sm">中文a>
<a class="btn btn-sm">Englisha>
form>
body>
html>
- 使用thymeleaf 需要引入 xmlns:th=“http://www.thymeleaf.org”
4.config/DruidConfig: Druid配置类,不用Druid可不用配置
@Configuration
public class DruidConfig {
@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","a");
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.1config/MyMvcConfig: 添加登录首页访问路径
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer configurer = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
};
return configurer;
}
}
5.domain/User: 实体类对应数据库表
public class User implements Serializable {
private Integer id;
private String username;
private String password;
自己生成set/get方法和toString方法
6.mapper/UserMapper: 持久层接口
public interface UserMapper {
public User findUser(@Param("username")String username, @Param("password")String password);//根据姓名密码查询用户
}
@Param 用于mybatis多个参数传入
7.service/UserService: 业务层接口
public interface UserService {
public User findUser(User user);//根据对象查询用户
}
7.1service/impl/UserServiceImpl: 业务层实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findUser(User user) {
User u = this.userMapper.findUser(user.getUsername(),user.getPassword());
return u;
}
}
8.springboot启动类:
@MapperScan("com.baidu.springboot_10_simpledemo.mapper")
@SpringBootApplication
public class Springboot10SimpledemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10SimpledemoApplication.class, args);
}
}
@MapperScan(“com.baidu.springboot_10_simpledemo.mapper”):扫描指定路径下所有mapper
或者在每一个mapper接口上加@Mapper注解
9.resorces/mybatis/mybatisConfig.xml: mybatis主配置文件
<configuration>
<typeAliases>
<package name="com.baidu.springboot_10_simpledemo.domain"/>
typeAliases>
configuration>
9.resorces/mybatis/mapper/mybatisConfig.xml: mybatis映射文件
<mapper namespace="com.baidu.springboot_10_simpledemo.mapper.UserMapper">
<select id="findUser" parameterType="map" resultType="user">
select * from user where username=#{username} and password=#{password}
select>
mapper>
parameterType=“map” :传入多个参数,传入参数类型可写map
username=#{username} and password=#{password} :mapper接口中用@Param注解过,#{}里面的值就是注解的内容
至此已全部完成,前端页面资源是在Bootstrap官网找的简单模板,如果需要可以自行下载!