springboot integrates mybatis
1, add pom reference
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java
2 application.properties
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
3 in resource Create the mybatis directory under the directory and create the mybatis-config.xml file
Create the mapper directory under the mybatis directory to store the mapper class interface file
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { List getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
model class file
package com.kerry.mapper; import java.util.List; import com.kerry.model.User; public interface UserMapper { List getAll(); User getOne(Integer id); void insert(User user); void update(User user); void delete(Integer id); }
userMapper.xml
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}
controller:
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 List getUsers() { 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); } }
Finally, add the scanning maper interface annotation
@SpringBootApplication @MapperScan("com.kerry.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
or in each It is also possible to add the @mapper annotation to an XXMapper class. You can choose one of the two. It is not necessary to add the @mapper annotation every time you write a mapper class
The above is the detailed content of How to integrate mybatis xml in springboot. For more information, please follow other related articles on the PHP Chinese website!
mybatis first level cache and second level cache
SpringBoot project building steps
What is the difference between ibatis and mybatis
How to configure database connection in mybatis
What is the working principle and process of mybatis
What are the differences between hibernate and mybatis
How to convert pdf to xml format
What is the difference between j2ee and springboot