目录
- 1.1、代码编写
- Demo类
- User类
- 启动类 APplication
- 1.2、配置类
- 查询语句的配置 mysql.yml
- springboot的配置 application.yml
- 日志的配置 logback.xml
- 数据库的配置 mybatis-config.xml
- 1.3、测试:
- 1.3.1、测试获取用户数
- 1.3.2、添加用户
- 1.3.3、数据的更新
- 1.3.4、数据的删除
1.1、代码编写
Demo类
import com.course.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Log4j
@RestController
@Api(value = "v1",tags = "这是我的第一个版本的demo")
@RequestMapping("v1")
public class Demo {
//首先获取一个执行sql语句的对象
@Autowired
private SqlSessionTemplate template;
@RequestMapping(value = "/getUserCount",method = RequestMethod.GET)
@ApiOperation(value = "可以获取到用户数",httpMethod = "GET")
public int getUserCount(){
return template.selectOne("getUserCount");
}
@RequestMapping(value = "/addUser",method = RequestMethod.POST)
public int addUser(@RequestBody User user){
int result = template.insert("addUser",user);
return result;
}
@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
public int updateUser(@RequestBody User user){
return template.update("updateUser",user);
}
@RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
public int delUser(@RequestParam int id){
return template.delete("deleteUser",id);
}
}
User类
@Data
public class User {
private int id;
private String name;
private String sex;
private int age;
}
启动类 APplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.annotation.PreDestroy;
@EnableScheduling
@SpringBootApplication
public class Application {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
Application.context = SpringApplication.run(Application.class,args);
}
@PreDestroy
public void close(){
Application.context.close();
}
}
1.2、配置类
查询语句的配置 mysql.yml
<?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.course">
<select id="getUserCount" resultType="Integer">
select count(*) from user;
</select>
<insert id="addUser" parameterType="com.course.model.User">
insert into user(id,name,age,sex)
values(#{id},#{name},#{age},#{sex})
</insert>
<update id="updateUser" parameterType="com.course.model.User">
update user set name=#{name},age=#{age}
where id=#{id}
</update>
<delete id="deleteUser" parameterType="Integer">
delete from user where id = #{id}
</delete>
</mapper>
springboot的配置 application.yml
#服务端口配置
server:
port: 8888
#日志配置
logging:
path: logs
file: mylog.log
#报告和数据库的配置
spring:
application:
name: report
datasource:
driver-class-name: com.mysql.jdbc.Driver #数据库驱动
# 数据库的地址
url: jdbc:mysql://localhost:3306/course?useSSL=false&useUnicode=true&characterEncoding=utf-8&servetTimeZone=Asia/Shanghai
username: root
password: 123456
mybatis:
# 做映射的 com.course.model包下的对象
type-aliases-package: com.course.model
#加载resources/mapper下的文件
mapper-locations:
- mapper/*
日志的配置 logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
格式化日式的时间,格式,路径等
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/>
<property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="CRAWLER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/event.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/event.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%msg%n</pattern>
</encoder>
</appender>
<logger name="com.business.intelligence.util.CrawlerLogger" level="INFO" additivity="false">
<appender-ref ref="CRAWLER_LOG"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
数据库的配置 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 映射到哪个包里面-->
<typeAliases>
<package name="com.course.model"/>
</typeAliases>
<!-- 映射到哪个sql文件里面-->
<mappers>
<mapper resource="mapper/mysql.xml"/>
</mappers>
</configuration>
1.3、测试:
1.3.1、测试获取用户数
1.3.2、添加用户
1.3.3、数据的更新