MyBatis-Plus学习-part1 Spring整合MP并实现插入

文件结构

在这里插入图片描述

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ezerbel.mp</groupId>
  <artifactId>mp01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mp01</name>
  <description>mp01</description>
  <dependencies>
  	<!--该依赖自动维护Mybatis 以及 MyBatis-spring -->
	<dependency>
		<groupId>com.baomidou</groupId>
		<artifactId>mybatis-plus</artifactId>
		<version>2.3</version>
	</dependency>
	
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.9</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>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.37</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.3.10.RELEASE</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-orm</artifactId>
		<version>4.3.10.RELEASE</version>
	</dependency>
	
  </dependencies>
  
  <build>
	  <plugins>
		<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-compiler-plugin</artifactId>
			    <version>3.6.1</version>
		 	<configuration>
		 	    <source>1.8</source>
		 	    <target>1.8</target>
		 	    <encoding>UTF8</encoding>
		 	</configuration>
		</plugin>
	  </plugins>
  </build>
  
</project>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--   <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers> -->
</configuration>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=1234

application.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- 数据源 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name ="driverClass" value="${jdbc.driver}"></property>
		<property name ="jdbcUrl" value="${jdbc.url}"></property>
		<property name ="user" value="${jdbc.username}"></property>
		<property name ="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 事务管理器 -->
	<bean id="dataSourceTransationManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>	
	</bean>
	
	<!-- 基于注解的事务管理器 -->
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
	
	<!-- 配置SqlSessionFactoryBean 
		mybatis 提供的class: org.mybatis.spring.SqlSessionFactoryBean
		mybatis-plus 提供的class: com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
	-->
	<!--  -->
	<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybaits-config.xml"></property>
		<!-- 别名出处 -->
		<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
	</bean>
	
	<!-- 配置mybatis 扫描mapper接口的路径 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ezerbel.mp.mapper"></property>
	</bean>
</beans>

Employee

package com.ezerbel.mp.beans;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;

/**
 * java Bean
 * @author Administrator
 *
 */
@TableName(value="tb_employee")
public class Employee
{	
	
	@TableId(value="id",type=IdType.AUTO)// value 指定数据库中对应列名, type 主键使用的策略
	private Integer id;
	private String lastName;
	private String email;
	private Integer gender;
	private Integer age;
	
	public Employee() {
		super();
	}
	
	public Employee(String lastName, String email, Integer gender, Integer age) {
		super();
		this.lastName = lastName;
		this.email = email;
		this.gender = gender;
		this.age = age;
	}

	public Employee(Integer id, String lastName, String email, Integer gender, Integer age) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.email = email;
		this.gender = gender;
		this.age = age;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", age="
				+ age + "]";
	}
}

EmployeeMapper

package com.ezerbel.mp.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.ezerbel.mp.beans.Employee;

/**
 * @author Ezerbel
 *	
 *	基于Mybatis: 在Mapper接口中编写CRUD相关方法,提供Mapper接口所对应的SQL映射文件以及方法对应的SQL语句
 *	基于MP: 让XxxMapper接口继承BaseMapper接口即可。
 */
public interface EmployeeMapper extends BaseMapper<Employee>{
	
}

TestMP

package com.ezerbel.mp.test;

import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ezerbel.mp.beans.Employee;
import com.ezerbel.mp.mapper.EmployeeMapper;


public class TestMP {
	
	private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper", EmployeeMapper.class);
	
	/**
	 * 通用插入操作
	 */
	@Test
	public void testCommonInsert() {
		Employee em = new Employee(null,"Lilith","Lilith@Sina.com",0,10);
		Integer insertRes = employeeMapper.insert(em);
		System.out.println("insertRes ======= " + insertRes);
	}
	
	@Test
	public void testDataSource() throws Exception {
		DataSource ds = ioc.getBean("dataSource",DataSource.class);
		Connection conn = ds.getConnection();
		System.out.println(conn);
		System.out.println("Finished!!!");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值