1.新建maven項(xiàng)目
創(chuàng)新互聯(lián)專業(yè)提供成都機(jī)柜租用服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購(gòu)買成都機(jī)柜租用服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。
先新建一個(gè)maven項(xiàng)目,勾選上creat a simple project,填寫groupid,artifactid
2.建立項(xiàng)目結(jié)構(gòu)
3.添加依賴
org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 MySQL mysql-connector-java junit junit org.springframework.boot spring-boot-maven-plugin
4.代碼編寫
在包的最外層添加啟動(dòng)類
package com.lee.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
實(shí)體類
package com.lee.test.pojo; import org.springframework.stereotype.Component; @Component public class User { private int id; private String name; private String telephone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
mapper接口
package com.lee.test.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.lee.test.pojo.User; @Mapper public interface UserMapper { ListgetUser(int id); }
service接口
package com.lee.test.service; import java.util.List; import com.lee.test.pojo.User; public interface UserService { public ListgetUser(int id); }
service接口實(shí)現(xiàn)
package com.lee.test.service; import java.util.List; import com.lee.test.pojo.User; public interface UserService { public ListgetUser(int id); }
controller層
package com.lee.test.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.lee.test.pojo.User; import com.lee.test.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") public ListgetUser(@RequestParam("id") int id) { return userService.getUser(id); } }
還有mapper.xml的實(shí)現(xiàn)
<?xml version="1.0" encoding="UTF-8"?>
最后是一些配置在application.properties中
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations: classpath:mapper/*.xml
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。