tk.mybatis springboot 可以使用 LTS
tk.mybatis
H:\java_work\work_tk\one_app 删除掉无关的文件
添加web接口
复制之前写的文件 BookController.java
package com.itheima.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String getById() {
System.out.println("springboot is running... ");
return "springboot is running...";
}
}
到 controller目录中去
然后 直接 执行 : 然后 直接 执行 : 然后 直接 执行 : http://127.0.0.1:8080/books
修改pom.xml
其中的 my add start 之间的两块地方是我自己添加的
<?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>
<groupId>com.itheima</groupId>
<artifactId>etc_controller_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>etc_controller_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- my add start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!--mysql8.0连接包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
<scope>runtime</scope>
</dependency>
<!--tk.mybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- my add end -->
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.itheima.EtcControllerDemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- my add 2 start -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
<!-- my add 2 end -->
</plugins>
</build>
</project>
新建目录和文件
generatorConfig.xml 的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 引入数据库连接配置的文件名-->
<properties resource="jdbc.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 配置 tk.mybatis 插件 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.itheima.tk.utils.MyMapper"/><!--获取MyMapper文件位置-->
</plugin>
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.username}"
password="${jdbc.password}">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.itheima.domain.table" targetProject="src/main/java"/>
<!-- 配置 Mapper XML 存放路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置 Mapper 存放路径 -->
<javaClientGenerator targetPackage="com.itheima.domain.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
<table schema="test_a5" tableName="user">
<!-- 默认为 false,在生成的sql中,table是否会加上 catalog 或者 schema -->
<property name="ignoreQualifiersAtRuntime" value="true"/>
<!-- mysql 配置 -->
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
修改配置文件
application.properties 内容如下:
# 应用名称
spring.application.name=etc_controller_demo
# 应用服务 WEB 访问端口
server.port=8080
## 数据库驱动:
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## 数据源名称
#spring.datasource.name=defaultDataSource
## 数据库连接地址
#spring.datasource.url=jdbc:mysql:
## 数据库用户名&密码:
#spring.datasource.username=***
#spring.datasource.password=***
spring.datasource.druid.url=jdbc:mysql:
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.driver-class-name= com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper
复制文件
jdbc配置文件 jdbc.properties
# MySQL 8.x: com.mysql.cj.jdbc.Driver
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql:
jdbc.username=root
jdbc.password=123456
主动创建一个文件
需要自己写,不是自动生成的
package com.itheima.tk.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
主动创建目录
箭头指向的文件是自动生成的
不要忘记在main函数前添加一句话
不要忘记添加 @MapperScan(value = “com.itheima.domain.mapper”)
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(value = "com.itheima.domain.mapper")
public class EtcControllerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EtcControllerDemoApplication.class, args);
}
}
建数据库文件:
user.sql
DROP DATABASE IF EXISTS test_a5;
CREATE DATABASE test_a5;
USE test_a5;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(36) NOT NULL,
`name` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`englishname` char(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_name_uindex` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'jack', '123456', 'jack', '14');
INSERT INTO `user` VALUES ('2', 'mike', '123456', 'mike', '16');
执行自动生成数据库文件
测试代码:
package com.itheima;
import com.itheima.domain.mapper.UserMapper;
import com.itheima.domain.table.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EtcControllerDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
}
@Test
void test1(){
User user = new User();
user.setId(1);
User user1 = userMapper.selectByPrimaryKey(user);
System.out.println(user1.getName());
}
}
添加create_time和 update_time
创建数据库 user.sql
DROP DATABASE IF EXISTS test_a6;
CREATE DATABASE test_a6;
USE test_a6;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(36) NOT NULL,
`name` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`englishname` char(255) NOT NULL,
`age` int(11) NOT NULL,
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT "createtime",
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT "update_time",
PRIMARY KEY (`id`),
UNIQUE KEY `user_name_uindex` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `user` VALUES ('1', 'jack', '123456', 'jack', '14',NOW(),NOW());
INSERT INTO `user` VALUES ('2', 'mike', '123456', 'mike', '16',NOW(),NOW());
navicat 设置数据库
更新文件
直接运行
会自动覆盖 3个文件
修改文件:
@Column(name = "create_time",updatable = false,insertable = false)
private Date createTime;
@Column(name = "update_time",updatable = false,insertable = false)
private Date updateTime;
测试数据库
package com.itheima;
import com.itheima.domain.mapper.UserMapper;
import com.itheima.domain.table.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EtcControllerDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
}
@Test
void test1(){
User user = new User();
user.setId(1);
User user1 = userMapper.selectByPrimaryKey(user);
System.out.println(user1.getName());
}
@Test
void insert3_test()
{
User user = new User();
user.setId(3);
user.setName("LiLei");
user.setAge(15);
user.setEnglishname("LiLei");
user.setPassword("123456");
int i = userMapper.insertSelective(user);
}
@Test
void update_test()
{
User user = new User();
user.setId(3);
user.setName("Poly_KK");
user.setAge(5);
user.setEnglishname("Poly");
user.setPassword("123456");
int update = userMapper.updateByPrimaryKey(user);
}
@Test
void delete3_test()
{
User user = new User();
user.setId(3);
int delete = userMapper.deleteByPrimaryKey(user);
}
}
insert 一条记录
update 一条记录
例子代码:
https://download.csdn.net/download/wowocpp/86245545
如何自己写 mysql 访问语句呢?
如果是有多个表需要更新维护,但是一个时间段内 只想更新一个文件
怎么办?
|