在IDEA创建springboot+mybatis+spring详细demo 在IDEA创建springboot+mybatis+spring 详细demo
开发环境
IDE: intellJ idea 2019.1
Tomcat: tomcat 8
JDK:Java 1.8
gradle: 2.14.1
步骤
1. 创建项⽬
在指定gradle的时候,可以指定本地的gradle路径,或者使⽤ recommanded 的gradle(创建时会⾃动下载)
线程对游戏影响大吗
2. 编辑 adle ⽂件
主要代码是:
//adle
//这是不指定本地gradle的做法,如果指定本地gradle,可以将 buildscript 这段删掉
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
maven { url 'maven.aliyun/nexus/content/groups/public/' }  //使⽤阿⾥云的库    }
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")    }c语言快速排序算法代码
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "application"
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url 'maven.aliyun/nexus/content/groups/public/' }
}
configurations {
providedRuntime
}
// 这段待会再解释,主要因为 idea IDE 编译时不会主动处理 xml ⽂件
task copyMapping(type: Copy) {
copy {
from("src/main/java") {
include("com/test/demo/mapper/*.xml")
}
into("${projectDir}/build/resources/main") //请确认你的编译输出路径是这样的
}
}
//引⼊相关依赖
dependencies {
compile('org.springframework.boot:spring-boot-starter-web') //必备
compile('org.springframework.boot:spring-boot-starter-thymeleaf') //必备
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
compile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter')
compile('mysql:mysql-connector-java:5.1.38')
compile('mysql:mysql-connector-java:5.1.38')
idea配置artifacts
compile group: 'batis', name: 'mybatis', version: '3.4.1'
compile group: 'batis', name: 'mybatis-spring', version: '1.3.0' }
3. 编辑代码
创建完后的项⽬结构如下图
3.1 User.java
//实体类,⽅法为getter/setter⽅法,没啥好说的
ities;
public class User {
private Integer userid;
private String name;
private Integer age;
private String address;
private String password;
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public Integer getUserid() {
return userid;
}
public Integer getAge() {
计算机的五大功能是什么return age;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
//这个是类强制转为String时调⽤的函数
@Override
public String toString() {
return "name:"+this.name+"\npassword:"+this.password;
}
}
3.2 UserMapper.java
// Mapper类,⽤以作为与持久层(数据库)打交道的类
st.demo.mapper;
ities.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> getAllUsers();
}
3.l
<!--本⽂件涉及到 Mybatis 的语法,请先⾏学习 -->
<!--本配置⽂件⽤以将 UserMapper进⾏配置,⾄于为什么使⽤配置⽂件,是为了解耦,⽅便修改 -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd">
<mapper namespace="st.demo.mapper.UserMapper">
<resultMap id="map" type="ities.User">
<result column="userid" property="userid" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
</resultMap>
<!-- id 的值必须要与 Mapper.java 中的某⼀⽅法的⽅法名相同,resultType 代表返回的类型是啥,Mybatis可以帮你⾃动装配-->    <select id="getAllUsers" resultType="ities.User" resultMap="map">
select * from user ;
</select>
</mapper>
3.4 TestService.java
// 定义服务的接⼝,⾯向接⼝编程
st.demo.service;
ities.User;
import java.util.List;
public interface TestService {
//    public Boolean validateUser(String username, String password);
public List<User> getAllUsers();
public User aUser();
}
3.5 TestSeviceImpl.java
/
/TestService 的实现类
inputbox怎么写st.demo.service.impl;
ities.User;
st.demo.mapper.UserMapper;
st.demo.service.TestService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class TestServiceImpl implements TestService {
//⾃动装配,注⼊(实际就是Spring框架将userMapper类实例化并赋值给 userMapper变量)
@Resource
private UserMapper userMapper;
@Override
public List<User> getAllUsers() {
AllUsers();
}
3.5 TestController.java
小苏打的学术名称叫什么ller;
st.demo.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@Controller
public class TestController {
@Resource
private TestService testService;
//这个⽅法处理的请求url
@RequestMapping("/")
// 说明这个⽅法返回的String是作为响应的内容返回,⽽不是返回给spring 进⾏字符串拼接以便返回 jsp/html 页⾯等    @ResponseBody
public String index(){
/
/看这⾥,只需要直接调⽤testService 即可获取数据,⽆需了解具体的实现细节,体现封装性、解耦以及⽅便拓展        AllUsers().toString();
}
@RequestMapping("/say")
@ResponseBody
public String say(){
return "helloWorld";
}
}
3.6 Main.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。