SpringBoot+MyBatis+MySQL环境搭建
用IDEA开始搭建一个Springboot项目:
1,选中“Spring Initializr”, 以默认选项为准
点击“Next”
修改“Group”和“Artifact”两项,点击“Next”
进入下一项:
以上3张截图的内容为主,点击“Next”,进入工程目录,如下图:
新建目录:controller、mapper、model、service
*目录介绍:
A.java目录下放入业务代码:
controller、mapper、model、service
B.resources是存放项目资源,
1.templates放置模板(html页面)
2.static放置css、图片、js等静态资源文件
application.properties文件中的内容如下:
#端口
server.port=8888
#设置spring-boot编码格式
spring.banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.messages.encoding=UTF-8
#视图层控制 用MVC方式访问templates下的HTML
#让Springboot支持jsp
spring.mvc.view.prefix=calsspath:/templates/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
#页面热加载,否则没法看到实时页面
spring.thymeleaf.cache =false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#MySql配置
spring.datasource.url =jdbc:mysql://localhost:3306/TEST
spring.datasource.username =root
spring.datasource.password =********
spring.datasource.driverClassName =com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.example.mydemo.model
# AOP 切面
# 添加@EnableAspectJAutoProxy。
spring.aop.auto=true
# 是否要创建基于子类(CGLIB)的代理(true),而不是基于标准的基于Java接口的代理(false)。
spring.aop.proxy-target-class=false
前提是假设MySQL中已有数据库“TEST”,含有表“person” 字段id 、name、age
如上面的配置文件信息,截图如下:
各个Java类文件内容:
MydemoApplication类:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@MapperScan("com.example.mydemo.mapper")
@EnableWebMvc
@SpringBootApplication
public class MydemoApplication {
public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args);
}
}
UserController类内容:
import com.example.mydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserServiceuserService;
@RequestMapping("/index")//过滤器
public String index(){
return "MyHtml";
}
@RequestMapping("/showUser/{id}")
public String selectUser(@PathVariable int id,Model model){
model.addAttribute("name",userService.selectUser(id).toString());
return "greets";
}
}
UserMapper类内容:
import com.example.mydemo.model.Person;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
@Repository
public interface UserMapper {
@Select("SELECT * FROM person WHERE id = #{id}")
Person selectUser(int id);//返回一个person对象
}
Person类:
public class Person {
private Integerid;
private Stringname;
private Integerage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "id="+id+", name="+name+", age="+age;
}
}
UserService类内容:
import com.example.mydemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.mydemo.model.Person;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class UserService {
@Autowired
public UserMapperuserMapper;
public Person selectUser(int id){
return userMapper.selectUser(id);
}
}
资源结构目录下的资源介绍:
templates目录下新建MyHtml.html和greets.html文件,static目录下新建test.css文件
MyHtml.html内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<link rel="stylesheet" href="/test.css" type="text/css"/>
</head>
<body>
<h1>Hello World</h1>
<img src="imag.png" alt="hello">
</body>
</html>
greets.html文件内容如下:
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
test.css目录如下:
body{
color:red;
}
**************************************************
启动工程后,在浏览器中输入:
出现如下界面:
在浏览器中输入:
http://localhost:8888/showUser/1
出现如下界面: