Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
建好后的项目结构 建包:Controller、mapper、model、service、impl
及resources下的 mapper文件夹,建好后如下
A、UserController.java
package com.example.demo.Controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping(value = "/user/")
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "all")
public List findAllUser() {
return userService.findAllUser();
}
}
B、UserMapper.java
package com.example.demo.mapper;
import com.example.demo.model.User;
import java.util.List;
public interface UserMapper {
List selectAllUser();
}
C、User.java
package com.example.demo.model;
public class User {
private Integer userId;
private String userName;
private String password;
private String phone;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
}
D、UserService.java
package com.example.demo.service;
import com.example.demo.model.User;
import java.util.List;
public interface UserService {
List findAllUser();
}
E、UserServiceImpl.java
package com.demo.service.impl;
import com.demo.mapper.UserMapper;
import com.demo.model.User;
import com.demo.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;//这里会报错,但是并不会影响
@Override
public List findAllUser() {
return userMapper.selectAllUser();
}
}
F、UserMapper.xml
点这里用SQL建表
CREATE TABLE user(
userId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
userName VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
INSERT INTO test.user (userId, userName, password, phone) VALUES (1, '1', '1', '1');
INSERT INTO test.user (userId, userName, password, phone) VALUES (2, '2', '2', '2');
A、DemoApplication.java 启动类 加上:@MapperScan("com.example.demo.mapper")
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")//TODO 将项目中对应的mapper类的路径加进来就可以了
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
#------------------配置虚拟文件路径--start------------------------------
#windows时启用
spring.http.multipart.location = E:/item/demo/src/main/resources/
#linux时启用
#spring.http.multipart.location = /root/item/demo/
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, file:${spring.http.multipart.location}
#-----------------------------------------配置虚拟文件路径--end-----------------------------------------------
#端口号
server.port = 8080
server.tomcat.uri-encoding = UTF-8
#-----------------------------------------数据库--start------------------------------------------------------
#基础配置
spring.datasource.name = lol
spring.datasource.url = jdbc:mysql://47.106.000.000:3306/lol?useUnicode = true&characterEncoding = UTF-8&allowMultiQueries = true
spring.datasource.username = root
spring.datasource.password = **********
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.max-idle = 10
spring.datasource.initial-size = 5
spring.datasource.druid.filters = stat
#获取连接等待超时时间
spring.datasource.max-wait = 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
spring.datasource.time-between-eviction-runs-millis = 60000
#一个连接在池中最小生存的时间
spring.datasource.min-evictable-idle-time-millis = 300000
spring.datasource.validation-query = SELECT 'x'
spring.datasource.test-while-idle = true
spring.datasource.test-on-borrow = false
spring.datasource.test-on-return = false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
spring.datasource.pool-prepared-statements = false
spring.datasource.max-pool-prepared-statement-per-connection-size = 20
#----------------------------------------------数据库--end----------------------------------------------------
#----------------------------------------------mybatis--start----------------------------------------------------
## 说明:classpath*:mapper/*.xml classpath要带*,冒号后不要有空格
mybatis.mapper-locations = classpath*:mapper/*.xml
mybatis.type-aliases-package =com.demo.model
一、扫描红码领取红包,二、使用红包付款,三、扫描蓝码付款,付款比红包多一分即可,谢谢您的鼓励!