springboot连接数据库

导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

在application中配置

spring.datasource.username=root
spring.datasource.password=****
spring.datasource.url=****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=***
mybatis.mapper-locations=classpath:mapper/*.xml

添加实体类

@Data
public class Bigman implements Serializable {
    private String name;

    private String phone;

    private String firm;

    private String forword;

    private String image;

    private static final long serialVersionUID = 1L;
}


添加dao层

@Component
@Mapper
public interface BigmanDao {
    int insert(Bigman record);

    int insertSelective(Bigman record);

    ArrayList<Bigman> select();
}

添加xml

<?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.example.demo.dao.BigmanDao">
  <resultMap id="BaseResultMap" type="com.example.demo.pojo.Bigman">
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="firm" jdbcType="VARCHAR" property="firm" />
    <result column="forWord" jdbcType="VARCHAR" property="forword" />
  </resultMap>
  <insert id="insert" parameterType="com.example.demo.pojo.Bigman">
    insert into bigman (`name`, phone, firm, 
      forWord)
    values (#{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{firm,jdbcType=VARCHAR}, 
      #{forword,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.example.demo.pojo.Bigman">
    insert into bigman
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="name != null">
        `name`,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="firm != null">
        firm,
      </if>
      <if test="forword != null">
        forWord,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="firm != null">
        #{firm,jdbcType=VARCHAR},
      </if>
      <if test="forword != null">
        #{forword,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="select" resultMap="BaseResultMap">
    select *from bigman
  </select>
</mapper>

在controller层自动装配后使用即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值