搭建Spring-Mybatis(推荐新手练手)

项目比较简单,主要是框架搭建。

首先使用创建maven项目,

第一步:添加需要的依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.4</version>
</dependency>
<!--此以来必须与spring-mvc依赖版本一致-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

<!--数据库连接池依赖-->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

第二步:创建mybatis的配置文件

url=jdbc:mysql://localhost:3306/ums?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=2
maxActive=10

第三步:在spring配置文件引入mybatis

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--此标签用声明一个bean的方式创建了一个bean,后续可以通过 #{id.XXX}获取配置文件信息 -->
    <util:properties id="dbConfig" location="classpath:db.properties"></util:properties>
    <!--配置数据源 创建连接池对象 -->
     <!--在这里我们无需像mybatis项目一样配置environments 进行数据源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="url" value="#{dbConfig.url}"></property>
        <property name="driverClassName" value="#{dbConfig.driverClassName}"></property>
        <property name="username" value="#{dbConfig.username}"></property>
        <property name="password" value="#{dbConfig.password}"></property>
        <property name="initialSize" value="#{dbConfig.initialSize}"></property>
        <property name="maxActive" value="#{dbConfig.maxActive}"></property>
    </bean>
    <!--扫描bean 找到mybatis的接口声明类 并且默认创建bean bean名称为类名,但首字母小写 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置接口文件在哪里 -->
        <property name="basePackage" value="com.zb.mapper"></property>
    </bean>
     <!--创建SqlSessionFactory  连接数据源与mapper.xml 类似于mybatis项目的中mybatis配置的mappers标签-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置sql语句mapper.xml文件的位置-->
        <property name="mapperLocations" value="classpath:mappers/*.xml"></property>
        <!---->
    </bean>
</beans>

对于最后一个bean我的理解如下:SqlSessionFactoryBean的作用是读取配置文件中关于mybatis数据源的配置,通过读取到的配置文件创建会话工厂,通过会话工厂创建会话,此后通过mapperLocations类似于mybatis项目中的mappers标签,找到mapper文件。等于进行了mybatis项目中返回结果集之前的所有步骤。

 

第四步:创建mybatis实现接口,需要注意这里类的位置需要与扫描器的相匹配

接口内部抽象方法名称必须与mapper.xml中的标签方法一一对应

package com.zb.mapper;

import com.zb.pojo.User;

public interface UserMapper {
    Integer insert(User user);
    Integer UpdateUser(User user);
}

第五步:创建mybatis的mapper.xml进行对数据库的增删改查,此处的mapper.xml与mybatis项目的mapper.xml作用一样。

需要注意此处mapper.xml的位置必须与spring-config中的SqlSessionFactoryBean位置一致,否则会找不到具体的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">
<!--当前xml 对应的接口     namespace必须与接口类的路径一致-->
<mapper namespace="com.zb.mapper.UserMapper">
    <!--对应接口中的抽象方法名称-->
    <insert id="insert">
         insert  into user values
         (#{id},#{username},#{password},#{age},#{phone},#{email})
    </insert>
    <update id="UpdateUser">
        update  user set email=#{email} where id=#{id}
    </update>
</mapper>

第六步:对应实体类

package com.zb.pojo;

import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)//开启链式加载
public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String phone;
    private String email;
    private Integer idDelete;
    private Integer dapartmentId;


}

第七步:最终测试

import com.zb.mapper.UserMapper;
import com.zb.pojo.User;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.SQLException;

public class TestDataSource {
    private static ClassPathXmlApplicationContext context;
    private static UserMapper userMapper;

    @Before
    public void doBefore() {
        System.out.println("测试准备");
        context=new ClassPathXmlApplicationContext("spring-config.xml");
        userMapper  =
                context.getBean("userMapper", UserMapper.class);
    }

    @Test
    public void TestInsert() throws SQLException {
        User user=new User();
        user.setId(5).setEmail("24395@qq.com");
        Integer i=userMapper.UpdateUser(user);
        System.out.println(i);
    }

    @After
    public void doAfter() throws SQLException {
        context.close();
        System.out.println("测试结束");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

封肃小熊熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值