在IDEA用SpringBoot模板引擎創(chuàng)建SpringBoot項目,勾選Web、MySQL、JPA、MyBatis。
在resource下新建application.yml文件,這里使用ymal來配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#使用druid數(shù)據(jù)源
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#指定實體所在的位置
type-aliases-package: cn.edu.hsxy.entity
#指定映射文件的位置,如果全部使用注解,沒有映射文件可以不寫
mapper-locations: classpath:mapper/*.xml
新建實體了User
package cn.edu.hsxy.entity;
public class User {
private int id;
private String name;
private String password;
/*
set/get方法
*/
}
實體類創(chuàng)建好了開始寫dao。
package cn.edu.hsxy.dao;
import ...
@Mapper
public interface UserDao {
int insert(User user);
User selectByPrimaryKey(Integer id);
int update(User user);
@Delete("DELETE FROM user WHERE id=#{id}")
int delete(Integer id);
}
- 需要在dao的所以接口上寫上@Mapper注解。如果不寫,可以在主函數(shù)上寫@MapperScan("cn.edu.hsxy.dao"),指向dao接口所在的包,掃描該包下的所有接口。
在resource下創(chuàng)建mapper包,放所有的映射文件,與之前application.yml文件的位置要一致。這里創(chuàng)建UserMapper.xml,如下.
<?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="cn.edu.hsxy.dao.UserDao">
<insert id="insert">
INSERT INTO
user
(name ,password)
VALUES
(#{name},#{password})
</insert>
<select id="selectByPrimaryKey" resultType="cn.edu.hsxy.entity.User">
SELECT *
FROM user
WHERE id=#{id}
</select>
<update id="update">
UPDATE
user
SET
name = #{name},
password = #{password}
WHERE
id=#{id}
</update>
</mapper>
- namespace的值就是到接口的全限定名,根據(jù)這個名找到接口所在的位置。
- id就是接口中的方法名。
- 如果在接口中的方法中寫好了@Select、@Delete等注解,則不需要再配置文件中寫映射了。
dao層寫好開始寫Service層,在service下創(chuàng)建UserService接口
package cn.edu.hsxy.service;
import cn.edu.hsxy.entity.User;
public interface UserService {
public User getUserById(int id);
boolean addUser(User user);
boolean updateUser(User user);
Integer deleteUser(Integer id);
}
寫一個UserServiceImpl實現(xiàn)上接口,去寫業(yè)務邏輯
package cn.edu.hsxy.service.impl;
import ...;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User getUserById(int id) {
return userDao.selectByPrimaryKey(id);
}
@Override
public boolean addUser(User user) {
boolean result = false;
try{
userDao.insert(user);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
@Override
public boolean updateUser(User user) {
Integer result = -1;
try{
result = userDao.update(user);
}catch (Exception e){
e.printStackTrace();
}
if(result == 1){
return true;
}
return false;
}
@Override
public Integer deleteUser(Integer id) {
return userDao.delete(id);
}
}
Service層寫好了開始寫Controller層。
package cn.edu.hsxy.controller;
import ...;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUser")
@ResponseBody
public User getUser(){
User user = new User();
user.setId(1);
user.setName("ssk");
user.setPassword("123456");
return user;
}
@ResponseBody
@RequestMapping("/showUser")
public User toIndex(int id){
return userService.getUserById(id);
}
@RequestMapping("/updateUser")
@ResponseBody
public User updateUser(User user){
boolean flag = userService.updateUser(user);
if(flag == true){
return user;
}
return null;
}
@ResponseBody
@RequestMapping("/deleteUser")
public Integer deleteUserById(Integer id){
return userService.deleteUser(id);
}
}
接下來就是主函數(shù)了,因為SpringBoot使用內(nèi)嵌Tomcat,使用主函數(shù)就可以運行了。
package cn.edu.hsxy;
import ... ;
@SpringBootApplication
//@MapperScan("cn.edu.hsxy.dao")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
- 因為在接口中已經(jīng)寫@Mapper注解了,所以@MapperScan可以不寫,建議使用@MapperScan注解。
源碼及數(shù)據(jù)庫文件分享 提取碼:ijrz