使用IDEA构建Spring boot2.0+版本+MyBatis(properties文件配置)
2019-01-23 17:09
801 查看
最近项目从SSM转SpringBoot,但是不是用MyBatis而是用Spring JDBC
这篇博文也算记录我SpringBoot的搭建
1、Intellij idea菜单栏File->new->project
选择左侧栏中spring initializr,右侧选择jdk版本,以及默认的Service URL,点击next。
2、然后填写项目的Group、Artifact等信息,然后点击next
3、我这边是选择了Web->Web和Core->Aspects ,然后点击next
4、填写Project name 等信息,然后点击Finish。
maven web项目就这样创建好了
配置pom.xml:
<!-- Springboot jar--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- mybatis jar --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
application.properties
spring.datasource.url:jdbc:mysql://数据库地址:3306/test?useUnicode=true&characterEncoding=utf-8&noDatetimeStringSync=true&serverTimezone=Hongkong spring.datasource.driverClassName:com.mysql.jdbc.Driver spring.datasource.username:root spring.datasource.password:root server.port=8081 mybatis.mapper-locations:classpath:mapper/*.xml #配置模型路径 mybatis.type-aliases-package=com.
然后把项目的entity,mapper,service,controller创建出来,整体架构如图:
entity里面User:
public class User { private int id; private String userName; public int getId() { return id; } public User setId(int id) { this.id = id; return this; } public String getUserName() { return userName; } public User setUserName(String userName) { this.userName = userName; return this; } }
mapper里面的UserMapper:
@Mapper public interface UserMapper { void add(User user); List<User> listAll(User user); }
service里面的UserService:
@Service public class UserService { @Autowired private UserMapper userMapper; public List<User> listAll(User user) { return userMapper.listAll(user); } public void add(User user){ userMapper.add(user); } }
controller里面的UserController:
@Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "index") @ResponseBody public List<User> index(User user){ return userService.listAll(user); } }
resoures->mappe文件里面的UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lwj.springboot.mapper.UserMapper"> <resultMap id="UserResultMap" type="com.lwj.springboot.entity.User"> <id column="id" property="id"/> <result column="user_name" property="userName"/> </resultMap> <insert id="add" parameterType="com.lwj.springboot.entity.User"> insert into user(user_name) values (#{userName}) </insert> <select id="listAll" parameterType="com.lwj.springboot.entity.User" resultMap="UserResultMap"> select * from user </select> </mapper>
SpringBoot整合Mybatis主要是在prop文件配置:
最后运行结果:
第一次写技术博文不足之处还请大家多多指正!这也算是我2018年的工作总结吧!
最后附上码云代码:https://gitee.com/ZEROjie/springboot
相关文章推荐
- spring boot:使用@ConfigurationProperties注解加载配置文件
- 第三节:SpringBoot使用properties配置文件实现多环境配置
- Spring/Maven/MyBatis配置文件结合properties文件使用
- Springboot属性文件application.properties配置大全,版本:1.5.4
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- 使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot、mybatis
- eclipse 使用maven 构建springboot 读取多个配置文件
- 使用Gradle构建SpringBoot工程系列:第四篇:项目初始化(编辑build.gradle文件,引入项目基本配置及依赖)
- Spring/Maven/MyBatis配置文件结合properties文件使用
- SpringBoot 使用properties配置文件实现多环境配置
- SpringBoot---使用Mybatis配置文件实现数据的增删查改
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- Spring Boot 使用@ConfigurationProperties注解获取配置文件中的值
- spring-boot集成mybatis问题1:IntellijIdea中j解决自动生成代码xml配置文件中出现“uri is not registered”问题
- Mybatis3.2.1使用例三:Mapper方式集成Spring、配置文件提供SQL
- SpringBoot学习之配置文件注入@ConfigurationProperties
- Spring Boot基础3-配置文件详解:Properties和YAML
- springboot 配置使用maven中的properties