IDEA安装插件Spring Assistant。
新建项目,选择Spring Assistant
选中如下依赖
自动生成的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.legion</groupId> <artifactId>hellomybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>HelloMybatis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
工程内容和上一篇基本一致:
public class Machine { private int id; private String mach_no; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMach_no() { return mach_no; } public void setMach_no(String mach_no) { this.mach_no = mach_no; } } package com.legion.pojo; public class ResultEntity { private String status; private String data; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
@Mapper public interface MachineMapper { public List<Machine> findAll(); public Machine isMachineIn(String mach_no); }
/*MachineService.jva*/ public interface MachineService { public List<Machine> findAll(); public Machine findMachine(String no); } /*MachineServiceImpl.java*/ @Service public class MachineServiceImpl implements MachineService { @Autowired private MachineMapper machine; @Override public List<Machine> findAll() { return machine.findAll(); } @Override public Machine findMachine(String no) { return machine.isMachineIn(no); } }
@RestController public class UserController { @Autowired private MachineService ms; @RequestMapping("/all") public List<Machine> findAll(){ return ms.findAll(); } @RequestMapping("/hello") public String show() { return "hello,world"; } /** * * @param machine * @return */ @RequestMapping(value="/testMachine",method=RequestMethod.GET) public ResultEntity test(@RequestParam(required=true) String machine,@RequestParam(required=true) String pass) { Machine m=ms.findMachine(machine); ResultEntity re=new ResultEntity(); if(!pass.equals("hello")) { re.setStatus("no_pass"); re.setData("wrong"); return re; } if(m!=null) { re.setStatus("bad"); re.setData("already exists"); }else { re.setStatus("ok"); re.setData("good to add one here"); } return re; } }
和对应Eclipse工程不一样的地方
第一点
原来mapper接口文件和xml放在同一个包中。
这里在project视图中,在resource目录下新建mapper目录(注意新建的是directory),所有xml文件放到该目录下。
然后在application.properties文件配置mapper的路径
mybatis.mapper-locations=classpath:mapper/*xml
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
第二点
spriingboot2使用的mysql-connector-java
是最新版8,所以驱动类名称由com.mysql.jdbc.Driver改成com.mysql.cj.jdbc.Driver,数据库名称后面需要接时区。