一、创建项目
1、File->new->project;
2、选择“Spring Initializr”,点击next;(jdk1.8默认即可)
3、编辑一下你的项目信息,group可不做修改,项目名可做修改;最终建的项目名为:one,src->main->java下包名会是:com->example->one;点击next;
4、Web下勾选Spring Web,(不同版本可能会是Spring Web Star);Template Englines勾选Thymeleaf;SQL勾选:MySQL Driver(我也勾选了Oracle Driver),JDBC API 和 MyBatis Framework四项;点击next;
5、选择项目路径,我用的是mac,所以和windows路径看上去和windows有点不同。点击finish,新窗口打开
6、打开项目以后,点击右下角的import change,开始导入maven依赖
7、在templates文件下新建index.html页面,作为启动的初始页面;
8、在com.example.one下新建controller文件夹,在controller文件夹下建一个简单的helloController类;(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)
package com.example.one.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 崔耀中
* @since 2020-05-15
*/
@Controller
public class helloController {
@RequestMapping("/index")
public String sayHello(){
return "index";
}
}
9、在resources文件夹下application中先配置DataSource基本信息,application文件有两种文件格式,一种是以.properties为后缀,一种是以.yml为后缀的,两种配置方式略有差别,详情可参考这个网址:;在这我是用.yml后缀的文件格式。右键application文件选择Refact,选择Rename,将后缀改为yml;
spring:
datasource:
name: test #数据库名
url: jdbc:mysql://localhost:3306/testdb #url
username: test #用户名
password: 123456 #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
10、下面启动OneAplication,项目已经可以启动,只是提示mybatis警告,我们下面会解决。
浏览器输入http://localhost:8080/,已经可以看到页面
二、接口示例
spring boot项目大概分为四层
(1)DAO层:包括Xx xMapper.java(数据库访问接口类),XxxMapper.xml(数据库连接实现);(也可用Dao命名)
(2)Bean层:也叫model层、模型层、entity层、实体层,就是数据库表映射的实体类,存放POJO对象;
(3)Service层:也叫服务层、业务层,包括XxxService.java(业务接口类),XxxServiceImpl.java(业务实现类);可以在service文件夹下新建impl文件夹来放业务实现类,也可以把业务实现类单独存放一个文件夹下,更清晰;
(4)Web层:也叫Controller层,实现与web前端的交互。
1、数据库信息
(1)数据库连接:test用户名,testdb为表空间
(2)创建用户表
-- 创建人员表
create table USER
(
id int
primary key,
name VARCHAR(32),
password VARCHAR(32)
);
insert into USER values (1,'one','one');
commit;
2、依照上面四层,我们创建目录结构如下:
2、代码展示
(1)在application配置文件中添加MyBatis配置:
spring:
datasource:
name: test #数据库名
url: jdbc:mysql://localhost:3306/testdb #url
username: test #用户名
password: 123456 #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
mybatis:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: com.example.one.bean #配置实体类
(2)pom.xml文件配置信息(maven自动配置,也可以手动加自己需要的配置)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>one</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>one</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)Bean实体类依照数据库,生成set和get方法
package com.example.one.bean;
/**
* @author 崔耀中
* @since 2020-05-17
*/
public class UserBean {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(4)DAO层访问数据库接口:
package com.example.one.mapper;
import com.example.one.bean.UserBean;
public interface UserMapper {
UserBean getInfo(String name,String password);
}
(5)DAO层访问数据库实现文件,注意<mapper>标签的namespace属性要填写 访问数据库接口类文件路径:
<?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.example.one.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.example.one.bean.UserBean">
SELECT * FROM user WHERE name = #{name} AND password = #{password}
</select>
</mapper>
(6)Service层业务接口类编写:
package com.example.one.service;
import com.example.one.bean.UserBean;
public interface UserService {
UserBean loginIn(String name,String password);
}
(7)Service层业务实现类编写,注意要注解@Service,注入DAO:
package com.example.one.service.serviceImpl;
import com.example.one.bean.UserBean;
import com.example.one.mapper.UserMapper;
import com.example.one.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 崔耀中
* @since 2020-05-17
*/
@Service
public class UserServiceImpl implements UserService {
//将DAO注入Service层
@Autowired
private UserMapper userMapper;
@Override
public UserBean loginIn(String name, String password){
return userMapper.getInfo(name,password);
}
}
(8)项目启动类要添加注解@MapperScan项目启动时扫描mapper接口,否则会报错找不到mapper文件:
package com.example.one;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.one.mapper")
public class OneApplication {
public static void main(String[] args) {
SpringApplication.run(OneApplication.class, args);
}
}
(9)编写测试类,测试能否获取数据库信息
package com.example.one;
import com.example.one.bean.UserBean;
import com.example.one.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class OneApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
UserBean userBean = userService.loginIn("one","one");
System.out.println("该用户的id为:");
System.out.println(userBean.getId());
}
}
(10)controller层,需要添加@Controller注解,注入Service服务
package com.example.one.controller;
import com.example.one.bean.UserBean;
import com.example.one.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author 崔耀中
* @since 2020-05-17
*/
@Controller
public class LoginController {
//将service注入web层
@Autowired
UserService userService;
@RequestMapping("/login")
public String show(){
return "login";
}
@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
public String login(String name,String password){
UserBean userBean = userService.loginIn(name,password);
if (userBean != null){
return "success";
}else {
return "error";
}
}
}
(11)html文件
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action="/loginIn" method="post">
账号:<input type="text" id="name" name="name"> <br>
密码:<input type="password" id="password" name="password"> <br>
<input type="submit" id="login" value="login">
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>登陆成功</h1>
</body>
</html>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登陆失败</h1>
</body>
</html>
2、运行OneApplication.java文件,启动项目,无任何WARN警告信息,进入浏览器输入localhost:8080/login
ok,到这里就结束了。希望本文对你会有所帮助