springboot+mybatis框架学习之环境搭建
一、运行环境
- jdk1.8;
- IDEA2018;
- springboot 2.3.2,改为2.0.1;
二、搭建步骤
目录结构:
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiangmu01.demo</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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> <!--默认版本太低,切换模板引擎版本--> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.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>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> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-joda</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-parameter-names</artifactId> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <!--引入thymeleaf引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--引入jqrery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency> <!--引入bootstrap--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
整合mybatis时,pom.xml文件修改注意事项:
(1)添加Jackson(json解析工具):
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
(2)修改springboot版本(不然会报错)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version> <!--改为2.0.1-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
(3)默认访问静态文件夹下的页面配置(配置文件中缺少这个,默认访问静态文件夹下的页面失效)
<groupId>com.xiangmu01.demo</groupId>
<artifactId>xiangmu01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
(3)可选内容(根据程序内代码添加)
- 分页插件
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2.application.yml内容
server: port: 8085 spring: datasource: name: mysql_test type: com.alibaba.druid.pool.DruidDataSource #druid相关配置 druid: #监控统计拦截的filters filters: stat driver-class-name: com.mysql.jdbc.Driver #基本属性 url: jdbc:mysql://127.0.0.1:3306/springbootdb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true username: root password: #配置初始化大小/最小/最大 initial-size: 1 min-idle: 1 max-active: 20 #获取连接等待超时时间 max-wait: 60000 #间隔多久进行一次检测,检测需要关闭的空闲连接 time-between-eviction-runs-millis: 60000 #一个连接在池中最小生存的时间 min-evictable-idle-time-millis: 300000 validation-query: SELECT 'x' test-while-idle: true test-on-borrow: false test-on-return: false #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.xiangmu01.demo.model
3.UserMapper.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.xiangmu01.demo.dao.UserDao" > <sql id="BASE_TABLE"> user_tb </sql> <sql id="BASE_COLUMN"> userId,userName,password,phone </sql> <insert id="insert" parameterType="com.xiangmu01.demo.model.UserDomain"> INSERT INTO <include refid="BASE_TABLE"/> <trim prefix="(" suffix=")" suffixOverrides=","> userName,password, <if test="phone != null"> phone, </if> </trim> <trim prefix="VALUES(" suffix=")" suffixOverrides=","> #{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, <if test="phone != null"> #{phone, jdbcType=VARCHAR}, </if> </trim> </insert> <select id="selectUsers" resultType="com.xiangmu01.demo.model.UserDomain"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> </select> </mapper>
三、实现代码
分层结构:
- model层:(实体类)存放实体类,与数据库字段保持一致,常用于处理返回值是,当作返回值类型,保存数据库字段;
- Dao层:(mapper层)封装了对某一张表的增删改查,一般写成接口,实现方法写在resource/mapper/***.xml文件中;
- Service层:对一个或者多个Dao进行封装,封装成一个服务;
- Controller层:负责请求转发,接收页面传输过来的参数,传给Service处理,接到返回值,再传给页面;
注意事项:
1.在启动类中:
@MapperScan("com.xiangmu01.demo.dao")//扫描指定包中的接口,非常关键,对应项目中Dao(Mapper)层的包路径;
2.继承接口的类要加上@Service注解,和@Autowired注解;例如:@Service(value = "userService");
3.Controller层中要在最前方加入@Controller注解;
具体代码:
1.启动类
package com.xiangmu01.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.xiangmu01.demo.dao")//扫描指定包中的接口 public class Xiangmu01Application { public static void main(String[] args) { SpringApplication.run(Xiangmu01Application.class, args); } }
2.model类中代码
package com.xiangmu01.demo.model; public class UserDomain { private Integer userId; private String userName; private String password; private String phone; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone == null ? null : phone.trim(); } }
3.Dao类代码
package com.xiangmu01.demo.dao; import com.xiangmu01.demo.model.UserDomain; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserDao { int insert(UserDomain record); List<UserDomain> selectUsers(); }
4.Service层代码
(1)service接口
package com.xiangmu01.demo.service.user; import com.github.pagehelper.PageInfo; import com.xiangmu01.demo.model.UserDomain; /** * Created by Administrator on 2018/4/19. */ public interface UserService { int addUser(UserDomain user); PageInfo<UserDomain> findAllUser(int pageNum, int pageSize); }
(2)imp文件下service类
package com.xiangmu01.demo.service.user.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.xiangmu01.demo.dao.UserDao; import com.xiangmu01.demo.model.UserDomain; import com.xiangmu01.demo.service.user.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by Administrator on 2020/8/2. */ @Service(value = "userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao;//这里会报错,但是并不会影响 @Override public int addUser(UserDomain user) { return userDao.insert(user); } /* * 这个方法中用到了我们开头配置依赖的分页插件pagehelper * 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可; * pageNum 开始页数 * pageSize 每页显示的数据条数 * */ @Override public PageInfo<UserDomain> findAllUser(int pageNum, int pageSize) { //将参数传给这个方法就可以实现物理分页了,非常简单。 PageHelper.startPage(pageNum, pageSize); List<UserDomain> userDomains = userDao.selectUsers(); PageInfo result = new PageInfo(userDomains); return result; } }
5.Controller层类
package com.xiangmu01.demo.controller; import com.xiangmu01.demo.model.UserDomain; import com.xiangmu01.demo.service.user.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * Created by Administrator on 2017/8/16. */ @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @ResponseBody @PostMapping("/add") public int addUser(UserDomain user){ return userService.addUser(user); } @ResponseBody @GetMapping("/all") public Object findAllUser( @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize){ return userService.findAllUser(pageNum,pageSize); } }
下一篇: Java指定保留小数位数的方法
推荐阅读
-
springboot+mybatis框架学习之环境搭建
-
Python环境搭建之OpenCV的步骤方法
-
MyBatis快速入门之环境搭建和单表映射
-
Android开发-之环境的搭建(图文详解)
-
Linux搭建Zookeeper环境之单机模式和集群模式配置 博客分类: 软件架构技术栈 linuxzk配置管理集群模式单机模式
-
Linux搭建Zookeeper环境之服务自启动 博客分类: 软件架构技术栈 LinuxZookeeper集群模式单机模式服务自启动
-
ibatis学习之搭建Java项目
-
monkeyrunner之安卓开发环境搭建教程(1)
-
ibatis学习之搭建Java项目
-
monkeyrunner之安卓开发环境搭建教程(1)