使用 mybatis generator 自动生成代码
参考:
Mybatis 自动生成代码 mybatis generator
https://blog.csdn.net/h356363/article/details/90649491
项目代码结构
配置POM 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringBoot2</artifactId>
<groupId>zz</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringBootMybatis</artifactId>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--目的:《可选》引入springboot 热启动,每次修改以后,会自动把改动加载,不需要重启服务了-->
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=9018
server.servlet.context-path=/mybatisdemo
spring.datasource.url = jdbc:mysql://localhost:3306/java10?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username = root
spring.datasource.password = Java20190713*yy
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
mybatis.typeAliasesPackage=org.spring.springboot.domain
#mybatis.mapperLocations=classpath:com/zz/mapping/*.xml,com/zz/EDGE/mapping/*.xml,com/zz/jwh/mapping/*.xml,com/zz/hc/mapping/*.xml,com/zz/simpleSpade/mapping/*.xml,com/zz/lsw/mapping/*.xml,com/zz/wsq/mapping/*.xml,com/zz/zcj/mapping/*.xml
mybatis.mapperLocations=classpath:mapping/*.xml
启动类配置Mapper路径
package com.zz;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 右键--》run as application 运行正启动类的main方法,就可以启动这个springboot项目。
SpringBoot 自带了 tomcat, 运行这个main方法 的时候,会同时启动tomcat
* @author jiyu
*
*/
@SpringBootApplication
@MapperScan("com.zz.mapper")
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
测试
对应controller
package com.zz.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.zz.util.KeyUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zz.entity.User;
import com.zz.service.UserService;
@RestController
@RequestMapping("user")
public class UserController {
@Resource
UserService userService;
@RequestMapping("add/{name}/{pwd}")
public User add(@PathVariable("name") String uname,@PathVariable("pwd") String pwd){
User user=new User();
user.setPasswd(pwd);
user.setUsername(uname);
user.setId(KeyUtil.genUniqueKey());
userService.add(user);
return user;
}
@RequestMapping("all")
public List<User> getAll(){
List<User> userlist=new ArrayList<>();
return userlist;
}
}
手动添加MyBatis方法,两种方式
- 第一种方式: 添加一个xml文件节点和接口方法
mapping/UserMapper.xml
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
</select>
com.zz.mapper.UserMapper
List<User> selectAll();
- 第二种方式: 只修改接口
采用了注解 @Select
@Select("select * from t_user where username=#{name}")
User selectByName(String name);
接口参考:
package com.zz.mapper;
import com.zz.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(String id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(String id);
List<User> selectAll();
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
@Select("select * from t_user where username=#{name}")
User selectByName(String name);
/**
* @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})")
* int add(Student student);
*
* @Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}")
* int update(Student student);
*
* @Delete("delete from student where sno=#{sno}")
* int deleteBysno(String sno);
*
* @Select("select * from student where sno=#{sno}")
* @Results(id = "student",value= {
* @Result(property = "sno", column = "sno", javaType = String.class),
* @Result(property = "name", column = "sname", javaType = String.class),
* @Result(property = "sex", column = "ssex", javaType = String.class)
* })
* Student queryStudentBySno(String sno);
*/
}
完整源码:
https://gitee.com/bseaworkspace/springboot2all