spring整合mybatis的使用---------整合第三方技术

前期相关jar包准备,使用Maven进行相关包的导入,即是pop.xml文件中

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

        <!-- spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- spring核心类 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- Spring tx:为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--这个jar 文件包含对Spring 对JDBC 数据访问进行封装的所有类-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- spring-mybatis整合包 -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 导入mybatis相关包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- 导入数据库连接池 -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

1.导入数据库user表,并生成相关类—User.java

package com.book.pojo;

import java.io.Serializable;
import lombok.Data;

/**
 * user
 * @author 
 */
@Data
public class User implements Serializable {
    private Integer id;

    private String username;

    private String password;

    private String email;

    private static final long serialVersionUID = 1L;
}

2.生成操作数据库的Dao接口-----UserDao.java

package com.book.DAO;

import com.book.pojo.User;

public interface UserDao {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

3.创建UserDao.xml将Dao相关操作数据库的操作,在这个Dao配置文件,进行书写相应sql语句。

<?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.book.DAO.UserDao">
  <resultMap id="BaseResultMap" type="com.book.pojo.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="email" jdbcType="VARCHAR" property="email" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, `password`, email
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.book.pojo.User" useGeneratedKeys="true">
    insert into user (username, `password`, email
      )
    values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.book.pojo.User" useGeneratedKeys="true">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        `password`,
      </if>
      <if test="email != null">
        email,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.book.pojo.User">
    update user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        `password` = #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.book.pojo.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      `password` = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

4. 创建连接数据库的db.properties文件,里面定义了连接数据库的相关参数

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:8080/ajax?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

5. 准备spring.xml文件,进行IOC和DI。将dao配置文件、数据库连接文件加入进去。使用了Duird的数据库连接池,进行数据库的连接。注意要加入spring-jdbc和spring-tx包。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解扫描 -->
<!--    <context:component-scan base-package="com"/>-->
    <!-- 加载数据库连接文件 -->
    <context:property-placeholder location="classpath:/db.properties"></context:property-placeholder>
    <!-- 准备数据库连接池 -->
    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- di注入属性 -->
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--    使用 spring和mybatis 的整合包 获得 sqlSessinFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 为其注入属性和相关参数 -->
        <property name="dataSource" ref="ds"></property>
         <!-- 为其注入数据库操作的配置文件UserDao.xml参数 -->
        <property name="mapperLocations" value="classpath:/generator/*Dao.xml"></property>
    </bean>
    <!-- 映射扫描Dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 映射扫描Dao类所在的包 -->
        <property name="basePackage" value="com.book.DAO"></property>
    </bean>
</beans>

6.Main类进行测试

import com.book.DAO.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        /*
        * 1.导入数据库user表,并生成相关类
        * 2.导入mybatis依赖
        * 3.准备spring.xml文件,进行IOC和DI。将dao配置文件加入进去,准备数据库连接文件
        * 注意要加入spring-jdbc和spring-tx包.
        * */
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserDao bean = context.getBean(UserDao.class);
        System.out.println(bean);
    }
}

结果

在这里插入图片描述

总结

  • spring的版本要保持一致,否则会出现版本不兼容的文件,本文用的spring版本统一是 5.1.9.RELEASE 版本
  • 数据库的连接文件db.properties中的数据库连接名不可以是username=root,而应该将username换成别的名字,或者写成jdbc.username。因为在使用username的时候,代码会把它认为是系统的username,从而匹配不是db.properties里面的username。
  • 使用IDEA自带的工具,一步生成过程1~3的文件时,注意打开下面的功能,否则会创建不成功。
  • 在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值