SpringBoot(四)----整合Mybatis

添加pom依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.1</version>
</dependency>

修改配置文件内容application.properties

server.port=8888
server.servlet.context-path= /springboot
#默认级别为info
logging.level.root=info
#com.springboot包下的级别为debug
logging.level.com.springboot=debug
logging.file.path= D:/my.log
#数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?characterEncoding=utf8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#设置包别名(在Mapper映射文件中直接使用实体类别名)
mybatis.type-aliases-package=com.springboot.springboot.entity
#指定系统到哪里去找mapper.xml映射文件
mybatis.mapper-locations=classpath:mappers/*.xml

创建一个实体类User

public class User {
    private String id;

    private String name;

    private Integer age;

    private Date birthday;

    private String sex;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }
}

创建一个Mapper接口

/***
 * springboot默认将@Mapper作为数据访问层
 * */
@Mapper
public interface UserMapper {
    List<User> findAll();
}

创建一个Mapper映射文件

<?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="com.springboot.springboot.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.springboot.springboot.entity.User">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="NAME" jdbcType="VARCHAR" property="name" />
    <result column="AGE" jdbcType="INTEGER" property="age" />
    <result column="BIRTHDAY" jdbcType="DATE" property="birthday" />
    <result column="SEX" jdbcType="VARCHAR" property="sex" />
  </resultMap>
  <sql id="Base_Column_List">
    ID, NAME, AGE, BIRTHDAY, SEX
  </sql>
  <select id="findAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
  </select>
</mapper>

创建业务层,service接口和实现类

public interface UserService {
    public List<User> findAll();
}
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

在启动类中添加扫描Mapper

@MapperScan(basePackages = {"com.springboot.springboot.mapper"})
@SpringBootApplication
public class SpringbootApplication{
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

创建一个Controller

@RestController
@RequestMapping("hi")
public class TestController {
    @Autowired
    private UserService userService;
    @RequestMapping("findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
}

启动程序进行测试

整合完成 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值