IDEA+Gradle创建Springboot项目整合mybatis
2019-01-22 11:14
731 查看
1.之前用的maven管理项目,现在公司用gradle,所以自己学习创建一个,特此记录一下(前提是你得配置了gradle)
1.首先是build.gradle文件
[code]buildscript { ext { springBootVersion = '2.1.2.RELEASE' } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() mavenLocal() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.inspur' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url = 'http://repo.inspur.com/artifactory/ecbu-gradle-release-local' credentials { username 'bss' password 'USTdYtIj' } } mavenCentral() mavenLocal() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2') compile('org.projectlombok:lombok:1.18.2') runtime('mysql:mysql-connector-java') compile('mysql:mysql-connector-java') testCompile('org.springframework.boot:spring-boot-starter-test') compile('com.alibaba:fastjson:1.2.5') compile('com.github.pagehelper:pagehelper:5.1.2') implementation 'org.springframework.boot:spring-boot-starter-freemarker' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
2. 创建数据库表
[code]CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `score` varchar(255) DEFAULT NULL, `class1` varchar(255) DEFAULT NULL, `school` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '熊大', '20', '100', '动物系', '北京大学'); INSERT INTO `student` VALUES ('2', '熊二', '21', '90', '动物系', '南京大学'); INSERT INTO `student` VALUES ('3', '光头强', '18', '100', '伐木系', '复旦大学');
3.创建项目结构
Controller
[code]package com.inspur.demo.controller; import com.inspur.demo.entity.Student; import com.inspur.demo.service.StudentService; import com.inspur.demo.vo.ResponseBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @GetMapping(value = "/all") public List<Student> getall(){ return studentService.findAll(); } }
Service
[code]package com.inspur.demo.service; import com.inspur.demo.entity.Student; import com.inspur.demo.mapper.StudentMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class StudentService { @Autowired private StudentMapper studentMapper; public List<Student> findAll() { return studentMapper.queryAll(); } }
Mapper
[code]package com.inspur.demo.mapper; import com.inspur.demo.entity.Student; import org.apache.ibatis.annotations.Select; import java.util.List; public interface StudentMapper { List<Student> queryAll(); }
Entity
[code]package com.inspur.demo.entity; import lombok.Data; @Data public class Student { private Integer id; private String name; private Integer age; private Integer score; private String class1; private String school; public Student(){} public Student(Integer id,String name,Integer age,Integer score,String class1,String school){ this.id=id; this.name=name; this.age=age; this.score=score; this.class1=class1; this.school=school; } }
4.resources目录下添加mybatis的配置文件(文件名是 mybatis-config.xml)
[code]<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="callSettersOnNulls" value="true"/> <setting name="logImpl" value="STDOUT_LOGGING"/> <setting name="cacheEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
日志配置文件( 文件名是 logback-spring.xml)
[code]<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Prudent>true</Prudent> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>log/logFile.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} -%msg%n</Pattern> </layout> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%15thread{15}] %-40logger{40} : %msg%n</pattern> </encoder> </appender> <logger name="org" level="INFO"/> <logger name="sun" level="INFO"/> <logger name="com.inspur" level="INFO" /> <root level="INFO"> <appender-ref ref="LOG_FILE" /> <!--<appender-ref ref="FILE" />--> </root> </configuration>
5.添加mapper.xml文件(名字是 StudentMapper.xml)
[code]<?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.inspur.demo.mapper.StudentMapper"> <select id="queryAll" resultType="com.inspur.demo.entity.Student"> select * from student </select> </mapper>
6.添加application.yml文件
[code]server: port: 8081 spring: application: name: demo datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 mybatis: mapper-locations: classpath:mapper/**/*.xml config-location: classpath:mybatis-config.xml
6.项目启动类
7.启动测试
8.大功告成
相关文章推荐
- IDEA + Gradle + Springboot Web + Mybatis项目实例创建
- 使用Idea创建ssm项目,SpringMVC+Spring+MyBatis+Maven整合
- IDEA 创建Spring + SpringMVC + MyBatis整合maven项目
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
- SpringBoot基础教程及框架整合(一)----IDEA创建SpringBoot项目
- 使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot、mybatis
- 初学springboot+Mybatis+MySQL+idea创建简单项目
- idea创建spring boot+mybatis(oracle)+themeleaf项目
- idea 创建的maven+spring+mybatis项目整合 报错无法创建bean
- 使用idea+gradle创建springboot项目
- IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架
- IDEA下创建Springboot,freemarker,mockserver,Gradle项目
- 基于gradle创建springBoot web项目(idea版本)
- 使用IDEA+GRADLE创建springboot项目
- idea创建spring boot+mybatis(oracle)+themeleaf项目
- [SpringBoot]IDEA快速构建SpringBoot应用项目(二)整合Mybatis + Druid
- IDEA下gradle创建springboot项目
- Idea创建spring boot 项目(4)整合Spring Data Jpa
- IDEA上搭建maven项目整合springboot+mybatis+shiro
- IDEA创建一个spring-boot整合Mybatis的模块时,遇到的问题