MybatisPlus

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生.

开发Mybatis-Plus

1、引入jar包或依赖
包括Mybatis-Plus的jar包,数据库驱动jar包,log4j日志包,c3p0连接池,spring-context,spring-orm

<dependencies>
        <!--mybatis-plus:包括mybatisplus, mybatis, mybatis-spring-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
    </dependencies>

2、创建数据库表和对应的实体类
在这里插入图片描述

public class Student {
    private Integer stuNo;
    private String stuName;
    private Integer stuAge;
    //getter/setter
    //构造器:一个无参,一个全参,一个不带id的

3、创建mybatis配置文件:mybatis.xml(没有具体配置信息,因为会放在spring中配置)
4、日志log4j.properties
在mybatis.xml中指定所用日志的具体实现

 <settings>
       <setting name="logImpl" value="LOG4J"/>
   </settings>

如果未指定时将自动查找,按照SLF4J——Aache Commons Logging——Log4j 2——Log4j——JDK logging的顺序进行查找。
5、配置数据库连接相关信息
6、配置spring :applicationContext.xml

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--数据源c3p0-->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--事务管理器-->
    <bean id="dataSourceTransactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="dataSourceTransactionManger"/>
    <!--SqlSessionFactoryBean: 操作MyBatis-->
<!--    <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">-->
    <!--MybatisSqlSessionFactoryBean: 操作MyBatis Plus-->
    <bean id="sessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <property name="typeAliasesPackage" value="com.cn.entity"/>
    </bean>
    <!--MyBatis只写接口不写实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cn.mapper"/>
    </bean>
</beans>

操作MyBatis-Plus只需要修改下面这里:
在这里插入图片描述

7、写接口

public interface StudentMapper extends BaseMapper<Student> {

}

继承BaseMapper,BaseMapper中封装了增删改查的方法,使用的时候只需要之间调用就可以,不用再写sql映射文件。

public class Test {
    public static void main(String[] args) throws SQLException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        StudentMapper studentMapper = applicationContext.getBean("studentMapper", StudentMapper.class);
        Student studnet = new Student("hellen",18);
        int insert = studentMapper.insert(studnet);
        System.out.println(insert);

    }
}
  • 注意:
@TableName("student")
public class Student {
    @TableId(value = "stu_no",type = IdType.AUTO)
    private Integer stuNo;
    @TableField(value = "stu_name")
    private String stuName;
    @TableField(value = "stu_age")
    private Integer stuAge;

实体类的名字如果与数据库表名不一致,要使用@TableName注解指定表名,MyBatis Plus默认支持驼峰写法,如果属性名是驼峰写法,默认可以对应上数据库表中的下划线。如果不一致,需要使用@TableField指定列名。数据库表的主键设置为自增,用 @TableId(value = “stu_no”,type = IdType.AUTO)指定名称和类型。
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值