@SpringBootApplication
package com.miao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
/**
* 主程序类
* @SpringBootApplication :这是一个SpringBoot应用
*
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//run.getBean("");
}
}
web、jdbc、mysql驱动、druid、mybatis
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.miaogroupId>
<artifactId>SpringBootartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.8.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
dependencies>
project>
spring:
datasource:
url: jdbc:mysql://localhost:3306/market
password: 123456
driver-class-name: com.mysql.jdbc.Driver
username: root
druid:
aop-patterns: com.atguigu.admin.* #监控SpringBean
filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙)
stat-view-servlet: # 配置监控页功能
enabled: true
login-username: admin
login-password: admin
resetEnable: false
web-stat-filter: # 监控web
enabled: true
urlPattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
filter:
stat: # 对上面filters里面的stat的详细配置
slow-sql-millis: 1000
logSlowSql: true
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
<configuration>
configuration>
我要查询的表 变量一定要对应好表
user.java
谨记:添加注解@Component
import org.springframework.stereotype.Component;
@Component
public class User {
private int uid;
private String uname;
private String sex;
private String upass;
public User(){
}
public User(int uid, String uname, String sex, String upass) {
this.uid = uid;
this.uname = uname;
this.sex = sex;
this.upass = upass;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", sex='" + sex + '\'' +
", upass='" + upass + '\'' +
'}';
}
}
谨记:添加注解@Mapper
package com.miao.mapper;
import com.miao.bean.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public User getById(Integer id);
}
注意:mapper的namespace千万别写错
这个里面可能犯错的地方很多一定要多注意
<mapper namespace="com.miao.mapper.UserMapper">
<select id="getById" resultType="com.miao.bean.User">
select * from users where uid = #{id}
select>
mapper>
@Service
public class UserService {
@Autowired
UserMapper usermapper;
public User getById(Integer id){
User user = usermapper.getById(id);
return user;
}
}
@RestController
public class Usercontroller{
@Autowired
UserService userService;
@GetMapping("/user")
public Map<String,Object> fun01(@RequestParam("id") Integer id){
Map<String,Object> map = new HashMap<>();
User user = userService.getById(id);
map.put("user",user);
return map;
}
}
访问http://localhost:8080/user?id=101
成功
总算成功了,中间出了好多问题
(操作的表不同类名也各不相同,我以我的为准说一下注意点)
application.yaml不用配置mybatis配置文件的位置和mapper映射文件的位置了
具体如下
application.xml
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
# mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
@Select("select * from users where uid = #{id}")
public User getById(Integer id);
其他的都不需要改变