提交 82892844 编写于 作者: H Henry He

feat: mysql database

上级 fe450179
# Ask Data
借助大语言模型, 通过提问对话方式, 获取数据图表.
...@@ -76,7 +76,15 @@ ...@@ -76,7 +76,15 @@
<artifactId>commons-text</artifactId> <artifactId>commons-text</artifactId>
<version>1.9</version> <version>1.9</version>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
......
package org.enthusa.askdata.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.enthusa.avatar.mybatis.helper.MapperHelper;
import org.enthusa.avatar.mybatis.helper.PageInterceptor;
import org.enthusa.avatar.mybatis.mapper.SqlMapper;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
/**
* @author henry
* @date 2023/6/24
*/
@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MapperConfiguration {
@Resource
private List<SqlSessionFactory> sqlSessionFactoryList;
@Resource
private ApplicationContext applicationContext;
@PostConstruct
public void addMapperInterceptor() {
MapperHelper mapperHelper = new MapperHelper();
PageInterceptor interceptor = new PageInterceptor();
applicationContext.getBeansOfType(SqlMapper.class);
mapperHelper.registerMapper(SqlMapper.class);
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
mapperHelper.processConfiguration(sqlSessionFactory.getConfiguration());
}
}
}
package org.enthusa.askdata.controller;
import org.enthusa.askdata.entity.BiPost;
import org.enthusa.askdata.mapper.BiPostMapper;
import org.enthusa.avatar.face.type.PageModel;
import org.enthusa.avatar.face.type.Result;
import org.enthusa.avatar.face.utils.ResultUtil;
import org.enthusa.avatar.face.utils.Validate;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/api")
public class BiPostController {
@Resource
private BiPostMapper biPostMapper;
@GetMapping("/posts")
public Result index(
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize
) {
PageModel<BiPost> pageModel = new PageModel<>();
pageModel.setPageAndPageSize(page, pageSize);
List<BiPost> postList = biPostMapper.selectByPage(pageModel);
pageModel.setList(postList);
return ResultUtil.success(pageModel);
}
@GetMapping("/posts/{id}")
public Result show(@PathVariable("id") Integer id) {
Validate.idValid("id", id);
BiPost post = biPostMapper.selectByPrimaryKey(id);
Validate.hasRecord("id", id, post);
return ResultUtil.success(post);
}
@PostMapping("/posts")
public Result create(@RequestBody @Valid BiPost biPost, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
Validate.isRecord(true, bindingResult.getFieldError().getDefaultMessage());
}
BiPost post = new BiPost();
BeanUtils.copyProperties(biPost, post);
biPostMapper.insertSelective(post);
return ResultUtil.success(post);
}
@PutMapping("/posts/{id}")
public Result update(@PathVariable("id") Integer id, @RequestBody @Valid BiPost biPost, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
Validate.isRecord(true, bindingResult.getFieldError().getDefaultMessage());
}
Validate.idValid("id", id);
BiPost post = biPostMapper.selectByPrimaryKey(id);
Validate.hasRecord("id", id, post);
BeanUtils.copyProperties(biPost, post);
biPostMapper.updateByPrimaryKey(post);
return ResultUtil.success(post);
}
}
package org.enthusa.askdata.entity;
import lombok.Data;
import java.util.Date;
@Data
public class BiPost {
/**
* id
*/
private Integer id;
/**
* title
*/
private String title;
/**
* create_time
*/
private Date createTime;
/**
* update_time
*/
private Date updateTime;
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
}
package org.enthusa.askdata.mapper;
import org.enthusa.askdata.entity.BiPost;
import org.apache.ibatis.annotations.Mapper;
import org.enthusa.avatar.mybatis.mapper.SqlMapper;
@Mapper
public interface BiPostMapper extends SqlMapper<BiPost, Integer> {
int insertSelective(BiPost record);
int updateByPrimaryKeySelective(BiPost record);
}
CREATE TABLE bi_post (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
<?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="org.enthusa.askdata.mapper.BiPostMapper">
<resultMap id="BaseResultMap" type="org.enthusa.askdata.entity.BiPost">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="title" property="title" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id, title, create_time, update_time
</sql>
<insert id="insertSelective" parameterType="org.enthusa.askdata.entity.BiPost"
useGeneratedKeys="true" keyProperty="id">
insert into bi_post
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="title != null">
title,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="org.enthusa.askdata.entity.BiPost">
update bi_post
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
package org.enthusa.askdata;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author henry
* @date 2023/6/24
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class AbstractTest {
}
package org.enthusa.askdata.mapper;
import org.enthusa.askdata.AbstractTest;
import org.enthusa.askdata.entity.BiPost;
import org.enthusa.avatar.face.type.PageModel;
import org.junit.Test;
import javax.annotation.Resource;
/**
* @author henry
* @date 2023/6/24
*/
public class BiPostMapperTest extends AbstractTest {
@Resource
private BiPostMapper biPostMapper;
@Test
public void test() {
PageModel<BiPost> pageModel = new PageModel<>();
pageModel.setOrders("id");
biPostMapper.selectByPage(pageModel).forEach(System.out::println);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册