Mybatis-Plus 实战完整学习笔记(二)------环境搭建

时间:2023-03-09 04:30:35
Mybatis-Plus 实战完整学习笔记(二)------环境搭建
 第二章    使用实例
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.搭建成功后

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

3. 项目搭建

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

建立一个maven项目

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

设置包名和项目名称

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

确认成功

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

新建包名

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

4.实体类创建

这里是标准的写法,根据阿里云Java手册规范写的,如果你需要可以下载其插件 网址:https://github.com/alibaba/p3c

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

源码:

 package com.baidu.www.mplus;

 /**
*
* @author liuyangos8888
*
* 实体类
*
*/
public class Employee { /**
* 字段的ID
*/
private Integer id ; /**
* 名字
*/
private String lastName; /**
* 邮箱
*/
private String email ; /**
* 性别
*/
private Integer gender ; /**
* 年龄
*/
private Integer 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;
}
}

5.SSM基础环境配置搭建

(1)全局mybaits-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> </configuration>

(2)log4j.xml日志配置,标红没问题

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p
%d{HH:mm :ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>

(3)数据库配置db.properties

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

(4) 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"
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">
<!-- 数据源 -->
<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="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 基于注解的事务管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/> <!-- 配置 SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean"> <!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"> </property> <!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">
</property>
</bean> <!--配置 mybatis 扫描 mapper 接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.baidu.www.mplus.mapper"> </property>
</bean>
</beans>

(5)目录结构

Mybatis-Plus 实战完整学习笔记(二)------环境搭建

6.测试类编写

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException; /**
* test
*/
public class TestMp {
private ApplicationContext iocContext = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test
public void context() throws SQLException { DataSource ds = iocContext.getBean("dataSource", DataSource.class);
Connection conn = ds.getConnection();
System.out.println(conn); }
}

7.Mybatisplus替代SqlSessionFactory,修改 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"
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">
<!-- 数据源 -->
<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="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 基于注解的事务管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/> <!-- 配置 SqlSessionFactoryBean -->
<!--<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">--> <!--&lt;!&ndash; 数据源 &ndash;&gt;-->
<!--<property name="dataSource" ref="dataSource"></property>--> <!--<property name="configLocation" value="classpath:mybatis-config.xml">--> <!--</property>--> <!--&lt;!&ndash; 别名处理 &ndash;&gt;-->
<!--<property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">-->
<!--</property>-->
<!--</bean>--> <!--&lt;!&ndash;修改为MybatisPlus配置&ndash;&gt;-->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"> </property> <!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">
</property>
</bean> <!--配置 mybatis 扫描 mapper 接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.baidu.www.mplus.mapper"> </property>
</bean>
</beans>

8.再次测试。无误初步MybaitsPlus的SSM环境配置成功。