Spring Boot与Mybatis
Spring Boot与Mybatis
Demo
项目结构
项目源码
package com.supremepole.b02springbootmybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class B02SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(B02SpringBootMybatisApplication.class, args);
}
}
package com.supremepole.b02springbootmybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author CodeCoderCoding
*/
@RestController
public class WebsiteController {
@Autowired
WebsiteService websiteService;
@GetMapping("/add-website")
public void addWebsite(){
Website website=new Website();
website.setId(1);
website.setName("supremepole");
website.setUrl("https://cs.supremepole.com");
websiteService.addWebsite(website);
Website website2=new Website();
website2.setId(2);
website2.setName("supremepole interview");
website2.setUrl("https://interview.supremepole.com");
websiteService.addWebsite(website2);
}
@GetMapping("/get-website")
public Website getWebsite(){
return websiteService.getWebsiteById(1);
}
@GetMapping("/update-website")
public void updateWebsite(){
Website website=new Website();
website.setId(1);
website.setName("supremepole algorithm");
website.setUrl("https://algorithm.supremepole.com");
websiteService.updateWebsite(website);
}
@GetMapping("/delete-website")
public void deleteWebsite(){
websiteService.deleteWebsiteById(2);
}
}
package com.supremepole.b02springbootmybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author CodeCoderCoding
*/
@Service
public class WebsiteService {
@Autowired
WebsiteMapper websiteMapper;
public int addWebsite(Website website) {
return websiteMapper.addWebsite(website);
}
public int updateWebsite(Website website) {
return websiteMapper.updateWebsite(website);
}
public int deleteWebsiteById(Integer id) {
return websiteMapper.deleteWebsiteById(id);
}
public Website getWebsiteById(Integer id) {
return websiteMapper.getWebsiteById(id);
}
public List<Website> getAllWebsites() {
return websiteMapper.getAllWebsites();
}
}
package com.supremepole.b02springbootmybatis;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author CodeCoderCoding
*/
@Mapper
public interface WebsiteMapper {
int addWebsite(Website website);
int deleteWebsiteById(Integer id);
int updateWebsite(Website website);
Website getWebsiteById(Integer id);
List<Website> getAllWebsites();
}
package com.supremepole.b02springbootmybatis;
/**
* @author CodeCoderCoding
*/
public class Website {
private int id;
private String url;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "id="+id+" ,name"+name+" ,url"+url;
}
}
<?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.supremepole.b02springbootmybatis.WebsiteMapper">
<insert id="addWebsite" parameterType="com.supremepole.b02springbootmybatis.Website">
INSERT INTO website(id,name,url) VALUES (#{id},#{name},#{url})
</insert>
<delete id="deleteWebsiteById" parameterType="int">
DELETE FROM website WHERE id=#{id}
</delete>
<update id="updateWebsite" parameterType="com.supremepole.b02springbootmybatis.Website">
UPDATE website set name=#{name},url=#{url} WHERE id=#{id}
</update>
<select id="getWebsiteById" parameterType="int" resultType="com.supremepole.b02springbootmybatis.Website">
SELECT * FROM website WHERE id=#{id}
</select>
<select id="getAllWebsites" resultType="com.supremepole.b02springbootmybatis.Website">
SELECT * FROM website
</select>
</mapper>
server.port=8081
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456