文章目录


1.Mybatis_Plus介绍

Mybatis_Plus为Mybatis的增强版,而不是替代版

2.集成Mybatis_Plus

Mybatis_Plus_环境搭建_xml

1.创建数据库

-- 创建库
CREATE DATABASE mp;
-- 使用库
USE mp;
-- 创建表
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1),
age int
);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@atguigu.com',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@atguigu.com',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@atguigu.com',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@atguigu.com',0,35);

2.创建响应的javabean

@Alias("emp")
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Integer age;

public Employee() {
}

public Employee(Integer id, String lastName, String email, Integer gender, Integer age) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.age = age;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", 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;
}
}

3.maven导入的jar包

    

com.baomidou

mybatis-plus
2.3



junit
junit
4.9



log4j
log4j
1.2.17



com.mchange
c3p0
0.9.5.2



mysql
mysql-connector-java
5.1.37



org.springframework
spring-context
4.3.10.RELEASE


org.springframework
spring-orm
4.3.10.RELEASE


4.加入log4j.xml文件























5.加入Spring的配置文件applicationContext.xml


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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring-1.2.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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


class="com.mchange.v2.c3p0.ComboPooledDataSource">






class="org.springframework.jdbc.datasource.DataSourceTransactionManager">



transaction-manager="dataSourceTransactionManager"/>

class="org.mybatis.spring.SqlSessionFactoryBean">



value="com.atguigu.mp.beans">



value="com.atguigu.mp.mapper">


6.jdbc.properties

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

7.mybatis-config.xml


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">






























8.写测试类

public class Test02 {
private ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");



@Test
public void test1()throws Exception{
DataSource bean = context.getBean(DataSource.class);
Connection connection = bean.getConnection();
System.out.println(connection);
}
}

Mybatis_Plus_环境搭建_sql_02