SpringBoot-MyBatis

文章目录

    • 环境搭建
    • 配置
    • 注解

环境搭建

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book
spring.datasource.username=root
spring.datasource.password=12345
# 配置mybatis规则
mybatis:#  config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xmlconfiguration:map-underscore-to-camel-case: true # 开启驼峰命名#  可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可

配置

UserMapper.xml


DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><select id="getUser" resultType="com.example.demo.bean.User">select * from  t_user where  id=#{id}select><select id="listUser" resultType="com.example.demo.bean.User">select * from  t_userselect>
mapper>

UserService

@Service
public class UserService {@AutowiredUserMapper userMapper;public User getUserById(int id){return userMapper.getUser(id);}
}

UserMapper

public interface UserMapper {public User getUser(int id);public List<User> listUser();}

MybatisController

@RestController
public class MybatisController {@AutowiredUserService userService;@GetMapping("/user")public User getById(@RequestParam("id") int id){return userService.getUserById(id);}
}

注解

BookMapper

public interface BookMapper {@Select("select * from  t_book where  id=#{id}")public Book getBookById(int book);
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部