码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis基础框架搭建

时间:2020-04-03 00:30:30      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:har   png   ash   iba   jdb   ace   private   where   连接池   

前言:基于springboot,使用mysql数据库,druid连接池

  1. 引入依赖

    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
    </dependency>
    
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
    </dependency>
    
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.14</version>
    </dependency>
    
  2. 配置application.yml

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/easy_mybatis? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        username: root
        password: ccc+022599
        type: com.alibaba.druid.pool.DruidDataSource
    mybatis:
      type-aliases-package: cn.ccb.pojo
      mapper-locations: classpath:mapper/*.xml
    
  3. 编写实体类、mapper接口、mapper映射文件

技术图片
mapper接口
@Repository
@Mapper
public interface UserMapper {
    public User queryUserByName(String name);
}
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private String username;
    private String password;
}
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="cn.ccb.mapper.UserMapper">
    <select id="queryUserByName" resultType="user" parameterType="string">
        select * from userinfo where username=#{name}
    </select>
</mapper>

mybatis基础框架搭建

标签:har   png   ash   iba   jdb   ace   private   where   连接池   

原文地址:https://www.cnblogs.com/codeccb/p/12615669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!