一.首先创建一个springboot项目(下图是创建的详细步骤)
1.file--->new----->Project
2.开始创建项目
3.
4.相关的一些依赖如果是有定好的需求可以直接在此添加,如果是后续要加的组件可通过pom文件去添加相关的依赖
5.创建完成
二. 开始整合mybatis
1.首先在项目的pom文件中添加springboot以及mybatis的相关依赖 (代码如下:)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
三.创建如下的结构文件
1.编写实体类 com.zp.zpdemo.Bean.User
package com.zp.zpdemo.Bean;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private String password;
private String perm;
public String getPerm() {
return perm;
}
public void setPerm(String perm) {
this.perm = perm;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", perm='" + perm + '\'' +
'}';
}
}
2.编写mapper接口 com.zp.zpdemo.Mapper.UserMapper
注意:需要使用@Mapper注解,不然SpringBoot无法扫描
package com.zp.zpdemo.Mapper;
import com.zp.zpdemo.Bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("select name,password from user where id = 1")
User select(String name, String password);
}
3.编写service接口 com.zp.zpdemo.service.UserService
注意:需要在接口实现类中使用@Service注解,才能被SpringBoot扫描,在Controller中使用@Resource注入
package com.zp.zpdemo.service;
import com.zp.zpdemo.Bean.User;
import com.zp.zpdemo.Mapper.UserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public User select(String name,String password){
return userMapper.select(name,password);
}
}
4.编写控制层接口 com.zp.zpdemo.Controller.UserController
package com.zp.zpdemo.Controller;
import com.zp.zpdemo.Bean.User;
import com.zp.zpdemo.service.UserService;
import com.zp.zpdemo.utils.Redisutils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class UserController {
@Resource
private UserService userService;
@Resource
private Redisutils redisutils;
@RequestMapping("/mybatis")
public @ResponseBody User user(String name , String password){
User user = userService.select(name,password);
redisutils.set(user.getName(),user.getPassword());
System.out.println(redisutils.get(user.getName())+"==========");
return user;
}
}
四. 配置文件application.properties
#访问根路径
#应用名称
spring.application.name=zpdemo
#访问端口号
server.port=8083
#编码格式
server.tomcat.uri-encoding=utf-8
#数据库相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.0.180:3306/springbootdemo?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC&transformedBitIsBoolean=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=1234
五.数据库表设计(因为有涉及redis,shiro,kafka的内容后续再上相关内容)
六. 启动项目 在浏览器中输入 :http://localhost:8083/mybatis即可访问
可关注微信公众号【冷眼看IT】获取更多技术干货: