小言_互联网的博客

SpringBoot整合MyBatisPlus

446人阅读  评论(0)

1、创建springboot项目或maven项目。

2、创建tbluser表

DROP TABLE IF
EXISTS tbluser;
CREATE TABLE
tbluser (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(20) DEFAULT NULL
COMMENT ‘用户名’,
pass_word varchar(20) DEFAULT NULL
COMMENT ‘密码’,
create_date datetime DEFAULT NULL,
address varchar(100) DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3、创建User类

public class User {

  private Integer userId;
  private String userName;
  private String passWord;
  private Date createDate;
  private String address;
  省略getter(),setter()方法

4、在pom.xml文件中添加依赖

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE


com.hp
springBoot_010
0.0.1-SNAPSHOT
springBoot_mybatisplus
hello world springBoot

  <!-- jdk版本 -->
  <properties>
      <java.version>1.8</java.version>
  </properties>

  <dependencies>
  
      <!--

springBoot启动器 -->

org.springframework.boot
spring-boot-starter-web

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      
      <!-- thymeleaf启动器 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      
      <!--

mybatisPlus启动器 -->

com.baomidou
mybatis-plus-boot-starter
2.3

      <!-- mysql数据库驱动 -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.45</version><!--$NO-MVN-MAN-VER$-->
      </dependency>
      
      <!-- druid数据库连接池 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.0</version>
      </dependency>

      <!-- fastjson

–>

com.alibaba
fastjson
1.2.23

  </dependencies>

  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

5、创建application.yml配置文件,放在src/main/resources下

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource

mybatis-plus:
type-aliases-package: com.hp.pojo
configuration:
map-underscore-to-camel-case: true

6、创建UserMapper接口,并继承BaseMapper接口

package com.hp.mapper;

import java.util.List;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.hp.pojo.User;

public interface UserMapper extends BaseMapper{

  /**
   * 查询所有用户信息
   */
  public List<User> selectUsers();
  
  /**
   * 新增用户用户信息
   */
  public void insertUser(User user);

}

7、创建IUserService接口

package com.hp.service;

import java.util.List;

import com.hp.pojo.User;

public interface IUserService {

       /**
        * 查询所有用户信息
        */
       public

List selectUsers();

       /**
        * 新增用户用户信息
        */
       public

void insertUser(User user);

       /**
        * 根据userId查询用户信息
        */
       public

User selectUserByUserId(Integer userId);
}

8、创建IUserService接口实现类UserService

package com.hp.service.impl;

import java.util.List;

import javax.annotation.Resource;

import
org.springframework.stereotype.Service;
import
org.springframework.transaction.annotation.Propagation;
import
org.springframework.transaction.annotation.Transactional;

import
com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hp.mapper.UserMapper;
import com.hp.pojo.User;
import com.hp.service.IUserService;

@Service
@Transactional(propagation=Propagation.REQUIRED)
public class UserService implements
IUserService{

       @Resource
       private

UserMapper userMapper;
/**
* 查询所有用户信息
*/
@Transactional(readOnly=true)
public
List selectUsers(){

                return userMapper.selectList(new

EntityWrapper<>());
}

       /**
        * 新增用户用户信息
        */
       public

void insertUser(User user) {

                userMapper.insert(user);
       }
       
       /**
        * 根据userId查询用户信息
        */
       @Transactional(readOnly=true)
       public

User selectUserByUserId(Integer userId) {
return userMapper.selectById(userId);
}
}

9、创建UserController类

package com.hp.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.RequestMapping;
import com.hp.pojo.User;
import com.hp.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

  @Resource
  private IUserService userService;
  
  @RequestMapping("userInfo")
  public String hello(ModelMap map) {
      
      List<User> userList = userService.selectUsers();
      map.put("userList", userList);
      
      return

“user/userInfo”;
}

  /**
   * 进入新增的页面
   */
  @RequestMapping(value="userEdit")
  public String userEdit(Integer userId,ModelMap map) {
      
      if(userId!=null) {
          
          //根据userId查询用户信息
          User user  = userService.selectUserByUserId(userId);
          map.put("user", user);
          map.put("userId", userId);
      }else {
          map.put("user", new User());
      }
      
      return

“user/userEdit”;
}

  /**
   * 保存用户信息
   */
  @RequestMapping(value="addUser")
  public String insertUser(User user,Integer userId) {
      
      userService.insertUser(user);
      
      return

“redirect:userInfo”;
}
}

10、创建启动器类App,注意,它所在的包必须是其它包的父包

package com.hp;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import
org.springframework.core.convert.converter.Converter;

@SpringBootApplication
@MapperScan(value=
{“com.hp.mapper”})
public class App {

  public static void

main(String[] args) {
SpringApplication.run(App.class, args);
}

  /**
   * 用于处理前台日期传到后台格式
   * @return
   */
  @Bean
   public Converter<String, Date> dateFormatConvert() {
      return new Converter<String, Date>() {
          @Override
          public Date convert(String source)

{

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = null;
try {
date = sdf.parse((String) source);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
}

11、在src/main/resources/templates下创建user文件夹,并创建userInfo.html文件

Insert title here
用户编号 用户姓名 用户密码 创建日期 用户地址

12、在src/main/resources/templates下创建user文件夹,并创建userEdit.html文件

Insert title here 用户姓名:
用户密码:
用户地址:
创建日期:

13、在浏览器中输入访问的地址,如下图所示

14、最后项目目录如下图所示


转载:https://blog.csdn.net/weixin_45471568/article/details/101558541
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场