mysql+xml+注释,springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)...
一、注解方式
pom
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
mysql
mysql-connector-java
org.projectlombok
lombok
1.16.10
說明: springboot版本: 2.1.5.RELEASE
application.properties
# mysql
spring.datasource.url=jdbc:mysql://212.64.xxx.xxx:3306/test?autoR&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.在啟動類中添加對 mapper 包掃描@MapperScan
package com.wangzaiplus.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@SpringBootApplication
@MapperScan("com.wangzaiplus.test.mapper")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
/**
* 跨域
* @return
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}
說明: springboot項目添加corsFilter解決跨域問題
也可以直接在 Mapper 類上面添加注解@Mapper
mapper
package com.wangzaiplus.test.mapper;
import com.wangzaiplus.test.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
@Results({
@Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
@Result(property = "password", column = "password")
})
List selectAll();
@Select("select * from user where id = #{id}")
@Results({
@Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
@Result(property = "password", column = "password")
})
User selectOne(Integer id);
@Insert("insert into user(username, password) values(#{username}, #{password})")
void insert(User user);
@Update("update user set username=#{username}, password=#{password} where id = #{id}")
void update(User user);
@Delete("delete from user where id = #{id}")
void delete(Integer id);
}
controller
package com.wangzaiplus.test.controller;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("users")
public String getAll() {
List users = userService.getAll();
return users.toString();
}
@GetMapping("{id}")
public String getOne(@PathVariable Integer id) {
User user = userService.getOne(id);
return user + "";
}
@PostMapping
public String add(User user) {
userService.add(user);
return "nice";
}
@PutMapping
public String update(User user) {
userService.update(user);
return "nice";
}
@DeleteMapping("{id}")
public String delete(@PathVariable Integer id) {
userService.delete(id);
return "nice";
}
}
說明: restful接口風(fēng)格
service
package com.wangzaiplus.test.service;
import com.wangzaiplus.test.pojo.User;
import java.util.List;
public interface UserService {
List getAll();
User getOne(Integer id);
void add(User user);
void update(User user);
void delete(Integer id);
}
impl
package com.wangzaiplus.test.service.impl;
import com.wangzaiplus.test.mapper.UserMapper;
import com.wangzaiplus.test.pojo.User;
import com.wangzaiplus.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List getAll() {
return userMapper.selectAll();
}
@Override
public User getOne(Integer id) {
return userMapper.selectOne(id);
}
@Override
public void add(User user) {
userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.update(user);
}
@Override
public void delete(Integer id) {
userMapper.delete(id);
}
}
說明: 僅供示例, 邏輯嚴(yán)謹(jǐn)性暫不考慮
pojo
package com.wangzaiplus.test.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
}
說明: @Data lombok
9.sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `unq_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
接口請求
add
update
delete
getOne
getAll
說明: 截圖請求參數(shù)id與數(shù)據(jù)庫id不對應(yīng)問題, 這是由于我接口文檔默認(rèn)值設(shè)為1或2的, 請求成功后接口管理工具自動刷新顯示默認(rèn)值id=1了, 所以看著好像不對, 實際沒問題
以上代碼均通過測試
二、xml方式
pom文件節(jié)點下添加
src/main/java
**/*.xml
說明: 如果不添加此節(jié)點mybatis的mapper.xml文件都會被漏掉, 會出現(xiàn)org.apache.ibatis.binding.BindingException Invalid bound statement (not found)異常
mapper java
package com.wangzaiplus.test.mapper;
import com.wangzaiplus.test.pojo.User;
import java.util.List;
public interface UserMapper {
List selectAll();
User selectOne(Integer id);
void insert(User user);
void update(User user);
void delete(Integer id);
}
說明: 不需要@Select @Insert等注解了
新建UserMapper.xml文件
id, username, password
SELECT
FROM user
SELECT
FROM user
WHERE id = #{id}
INSERT INTO user(username, password) VALUES (#{username}, #{password})
UPDATE user SET
username = #{username},
password = #{password}
WHERE id = #{id}
DELETE FROM user WHERE id =#{id}
說明: 相當(dāng)于將上個版本@Select @Insert注解用xml配置文件形式代替而已
其他都不需要修改
原文出處:https://www.cnblogs.com/wangzaiplus/p/10899962.html
總結(jié)
以上是生活随笔為你收集整理的mysql+xml+注释,springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx给php做统一入口,Nginx
- 下一篇: 私家车合乘系统 matlab,私家车贴上