当前位置:网站首页>第 2 章 集成 MP
第 2 章 集成 MP
2022-06-27 20:58:00 【String-int】
2.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','[email protected]',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','[email protected]',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','[email protected]',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','[email protected]',0,35);
2.2 创建 javaBean
public class Employee {
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; }
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email
+ ", gender=" + gender + ", age="
+ age + "]"; }
2.3 依赖配置
- 在 pom.xml 中加入对 MP、Spring、连接池、Junit、Mysql 驱动等依赖
<!-- mp 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- spring -->
<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>
特别说明: Mybatis 及 Mybatis-Spring 依赖请勿加入项目配置,以免引起版本冲突!!!
Mybatis-Plus 会自动帮你维护!
- 加入 MyBatis 的全局配置文件
<?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>
- 加入 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{MM-dd 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>
- 加入 db.properties 连接信息配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=1234
- 加入 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"
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 id="sqlSessionFactoryBean"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="classpath:mybatis-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.atguigu.mp.mapper"></property>
</bean>
</beans>
2.4 测试
- 测试 Spring-Mybatis 的环境,保证 OK
private ApplicationContext iocContext = new
ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testEnvironment() throws Exception{
DataSource ds = iocContext.getBean("dataSource",DataSource.class);
Connection conn = ds.getConnection();
System.out.println(conn);
}
2.6 集成 MP
- Mybatis-Plus 的集成非常简单,对于 Spring,我们仅仅需要把 Mybatis 自带的
MybatisSqlSessionFactoryBean 替换为 MP 自带的即可。
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
</bean>
边栏推荐
- [js]var, let,const 的区别
- 支持删除,更新任意结点的优先级队列
- The National University of Singapore 𞓜 uses model free reinforcement learning to evaluate the energy efficiency of the energy efficiency data center
- OData - API using SAP API hub in SAP S4 op
- "Top stream Aidou manufacturing machine" cooperates with four industrial capitals to become LP
- ABAP essay - material master data interface enhancement - tab enhancement
- How to set the enterprise wechat group robots to send messages regularly?
- 「R」 Using ggpolar to draw survival association network diagram
- Discuz淘宝客网站模板/迪恩淘宝客购物风格商业版模板
- [electron] basic learning
猜你喜欢
Netease cloud lost its "feelings" card
思源笔记订阅停止直接删云端数据嘛?
2022年PMP项目管理考试敏捷知识点(3)
【微服务|Sentinel】sentinel数据持久化
webserver流程图——搞懂webserver各模块间调用关系
Senior headhunting team manager: interviewed 3000 consultants, summarized and organized 8 commonalities (Mao Sheng)
跨系统数据一致性问题解决方案汇总
捷码赋能案例:湖南天辰产研实力迅速提升!实战玩转智慧楼宇/工地等项目
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
vmware虚拟机桥接连通
随机推荐
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
Web worker introduction and use cases
virtualbox扩展动态磁盘大小的坑
Typora 1.2.5等版本下载
跨系统数据一致性问题解决方案汇总
用pytorch进行CIFAR-10数据集分类
EXCEL 打印设置公共表头
在线JSON转PlainText工具
Sentinel
Swing UI——容器(一)
clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配
mongodb基础操作之聚合操作、索引优化
Getting started with pytorch
Practice torch FX: pytorch based model optimization quantization artifact
Spug - 轻量级自动化运维平台
Download versions such as typora 1.2.5
Spark bug practice (including bug:classcastexception; connectexception; NoClassDefFoundError; runtimeException, etc.)
发射,接收天线方向图