idea创建Springboot+mybatis+mysql
- 2019 年 10 月 8 日
- 筆記
首先找到File-new-Project找到Spring Initializr

然后点击next,填写好后继续next

选择左边的列web,右边列勾选web框

然后勾选需要的mysql mybatis等

如果前面没有修改工程名称,可以在这一界面更改,然后点击finish。

项目开始生成,界面右下角回弹出一个小方框,点击Enable Autop-Import ,idea 会自动下载jar包,时间比较长 (5分钟左右)

6、等待片刻后,项目格式如下图。controller,mapper,model,service是后来我新建的。

构建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 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.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 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.3.1</version> </dependency> <!-- mysql --> <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>
创建application.properties文件
#基本配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/liujilu?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=用户名 spring.datasource.password=密码 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver #使用mysql spring.jpa.database = mysql #是否显示sql语句 spring.jpa.show-sql=true #mybatis配置 mybatis.config-location=classpath:mybatis-config.xml // 配置文件位置 mybatis.typeAliasesPackage=com.example.demo.model mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
user实体类
package com.example.demo.model; public class User { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
创建Usermapper 接口
package com.example.demo.mapper; import com.example.demo.model.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<User> selectList(); }
创建接口 UserService
package com.example.demo.service; import com.example.demo.model.User; import java.util.List; public interface UserService { public List<User> selectList(); }
创建UserServiceImpl
package com.example.demo.service; import com.example.demo.mapper.CoachMapper; import com.example.demo.mapper.UserMapper; import com.example.demo.model.Coach; import com.example.demo.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper uMapper; @Override public List<User> selectList() { return uMapper.selectList(); } }
在resource目录下创建mybatis文件下、在该文件下创建mapper文件夹、然后在该文件夹下创建User.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.mapper.UserMapper"> <select id="selectList" resultType="user"> select * from user </select> </mapper>
创建controller
package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class TesrController { @Autowired private CoachService coachService; @Autowired private UserService userService; @RequestMapping("/hello") public String hello() { return "hello world3"; } @RequestMapping("/selectList") public List<User> selectList() { return userService.selectList(); } }
最后附上启动器的代码
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
项目结构图


启动项目在浏览器输入:http://localhost:8080/selectList ,如下图
