springboot集成mybatis
1. pom.xml中添加依赖包
<!-- jdbc --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2. 配置文件application.properties添加jdbc连接方式
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3. 添加mapper(注解方式)
public interface UserMapper { @Select("select id,user_code as code,user_name as username,user_pwd as password,update_tmsp as updateTime " + " from m_user " + " where user_name = #{name}") public User findUser(@Param("name") String name); }
4. controller中注入mapper
@Autowired private UserMapper userMapper; @RequestMapping("/queryUser") public User getUser(String name){ return userMapper.findUser(name); }
5. 启动程序app中添加mapper包扫描
@ComponentScan(basePackages={"com.king.controller"}) @MapperScan(basePackages = "com.king.mapper") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
本文来自博客园,作者:硅谷工具人,转载请注明原文链接:https://www.cnblogs.com/30go/p/8439688.html