springboot mengintegrasikan mybatis
1, tambah rujukan pom
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java
2 application.properties
3 dalam sumber Buat direktori mybatis di bawah direktori dan cipta fail mybatis-config.xml
mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.kerry.model 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 = 123456
Buat direktori mapper di bawah direktori mybatis untuk menyimpan fail antara muka kelas mapper
fail kelas model
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { ListgetAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
userMapper.xml
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { ListgetAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
pengawal:
id, name, age, address INSERT INTO user (id,name,age,address) VALUES (#{id},#{name}, #{age}, #{address}) UPDATE user SET name = #{name}, age = #{age}, address = #{address} WHERE id = #{id}DELETE FROM user WHERE id =#{id}
Akhir sekali, tambah anotasi antara muka pemeta imbasan
rreeeke kelas permulaan atau tambahkannya pada setiap. Anda juga boleh menambahkan anotasi @mapper pada setiap kelas XXMapper Anda boleh memilih salah satu daripada dua
Sebagai contoh,
package com.kerry.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.kerry.model.User; import com.kerry.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/getUsers") public ListgetUsers() { List users=userMapper.getAll(); return users; } @RequestMapping("/getUser") public User getUser(Integer id) { User user=userMapper.getOne(id); return user; } @RequestMapping("/add") public void save(User user) { userMapper.insert(user); } @RequestMapping(value="update") public void update(User user) { userMapper.update(user); } @RequestMapping(value="/delete/{id}") public void delete(@PathVariable("id") Integer id) { userMapper.delete(id); } }
Atas ialah kandungan terperinci Bagaimana untuk mengintegrasikan mybatis xml dalam springboot. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!