1、pom.xml
<?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.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 指定打包位war包 --> <packaging>war</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- Spring-web的启动器依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!--配置jdbc的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- <version>8.0.11</version> --> </dependency> <!--配置druid的依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--配置PageHelper分页插件启动器依赖--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency> <!-- thymeleaf模板 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </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> <!-- <resource> 指定处理 src/main/resources目录下的那些资源 <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.yaml</include> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.html</include>//加入html的文件 </includes> </resource> --> </project>
2、application.properties
#u4FEEu6539u7AEFu53E3 server.port=8080 spring.mvc.view.prefix=/templates/ spring.mvc.view.suffix=.html # u8FDEu63A5u56DBu5927u53C2u6570 spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root # u53EFu7701u7565uFF0CSpringBootu81EAu52A8u63A8u65AD #spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:com.example.demo.dao.*.xml spring.datasource.hikari.idle-timeout=60000 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.minimum-idle=10 logging.config=classpath:logback-spring.xml #logging.level.root=info
3、logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,%i索引【从数字0开始递增】,,, --> <!-- appender是configuration的子节点,是负责写日志的组件。 --> <!-- ConsoleAppender:把日志输出到控制台 --> <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d %p (%file:%line)- %m%n</pattern> <!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 --> <!--<charset>GBK</charset>--> </encoder> </appender> <!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 --> <!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是sys.log --> <!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过1KB时,对当前日志进行分割 重命名--> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- <File>sys.log</File>--> <File>H:log/sys.log</File> <!-- rollingPolicy:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。 --> <!-- TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 活动文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 --> <!-- 文件名:log/sys.2017-12-05.0.log --> <fileNamePattern>log/sys.%d.%i.log</fileNamePattern> <!--<fileNamePattern>sys.%d.%i.log</fileNamePattern>--> <!-- 每产生一个日志文件,该日志文件的保存期限为30天 --> <maxHistory>30</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 --> <maxFileSize>300KB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <!-- pattern节点,用来设置日志的输入格式 --> <pattern> %d %p (%file:%line)- %m%n </pattern> <!-- 记录日志的编码 --> <charset>UTF-8</charset> <!-- 此处设置字符集 --> </encoder> </appender> <!-- 控制台输出日志级别 --> <root level="INFO"> <appender-ref ref="Console" /> </root> <!-- 指定项目中某个包,当有日志操作行为时的日志记录级别 --> <!-- com.appley为根包,也就是只要是发生在这个根包下面的所有日志操作行为的权限都是DEBUG --> <!-- 级别依次为【从高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE --> <logger name="com.example.demo" level="INFO"> <appender-ref ref="Console" /> </logger> </configuration>
4、com.example.demo.JdbcConfig.java
package com.example.demo; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource; /** * * @author admin * */ @Configuration //@PropertySource("classpath:jdbc.properties") public class JdbcConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.driverClassName}") private String driverClassName; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource1() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); return dataSource; } }
5、com.example.demo.DemoApplication.java
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; //import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; //import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication @MapperScan(value = "com.example.demo.dao") //@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
6、com.example.demo.controller.UserController.java
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.User; import com.example.demo.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") public User getUser() { String idCard = "3306"; User user = userService.getUser(idCard); return user; } }
7、com.example.demo.service.UserService.java
package com.example.demo.service; import com.example.demo.entity.User; public interface UserService { User getUser(String idCard); }
8、com.example.demo.service.impl.UserServiceImpl.java
package com.example.demo.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.dao.UserDao; import com.example.demo.entity.User; import com.example.demo.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public User getUser(String idCard) { return userDao.getUser(idCard); } }
9、com.example.demo.dao.UserDao.java
package com.example.demo.dao; import org.apache.ibatis.annotations.Mapper; //import org.springframework.stereotype.Component; import com.example.demo.entity.User; @Mapper //@Component public interface UserDao { User getUser(String idCard); }
10、com.example.demo.dao.UserDao.xml
<?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.demo.dao.UserDao"> <select id="getUser" parameterType="String" resultType="com.example.demo.entity.User"> select id_card as idCard, user_name as userName, pwd from user1 where id_card = #{idCard} </select> </mapper>
11、com.example.demo.entity.User.java
package com.example.demo.entity; public class User { private String idCard; private String userName; private String pwd; public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
12、sql语句
/****产品信息表****/ CREATE TABLE T_PRODUCT( id INT(12) NOT NULL AUTO_INCREMENT COMMENT '编号', product_name VARCHAR(60) NOT NULL COMMENT '产品名称', stock INT(10) NOT NULL COMMENT '库存', price DECIMAL(16,2) NOT NULL COMMENT '单价', VERSION INT(10) NOT NULL DEFAULT 0 COMMENT '版本号', note VARCHAR(256) NULL COMMENT '备注', PRIMARY KEY (id)); /****产品信息表****/ CREATE TABLE T_PURCHASE_RECODE( id INT(12) NOT NULL AUTO_INCREMENT COMMENT '编号', user_id INT(12) NOT NULL COMMENT '用户编号', product_id INT(12) NOT NULL COMMENT '产品编号', price DECIMAL(16,2) NOT NULL COMMENT '数量', quantity INT(12) NOT NULL COMMENT '数量', SUM DECIMAL(16,2) NOT NULL COMMENT '总价', purchase_date TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '购买日期', note VARCHAR(512) NULL COMMENT '备注', PRIMARY KEY (id)); CREATE TABLE user1( id_card VARCHAR(18) NOT NULL COMMENT '身份证号', user_name VARCHAR(52) NOT NULL COMMENT '账号', pwd VARCHAR(32) NOT NULL COMMENT '密码', PRIMARY KEY (id_card)); INSERT INTO user1 (id_card, user_name, pwd) VALUE('3306','用户1','111111'); SELECT * FROM user1;