当前位置:网站首页>第 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>
边栏推荐
- How to start ID from 1 after MySQL deletes a table
- 新加坡国立大学|采用无模型强化学习方法评估能源效益数据中心的节能情况
- 第一性原理(最优解理论)
- 使用同花顺手机炒股安全吗?
- 实践torch.fx:基于Pytorch的模型优化量化神器
- 陈云pytorch学习笔记_用50行代码搭建ResNet
- Spark BUG實踐(包含的BUG:ClassCastException;ConnectException;NoClassDefFoundError;RuntimeExceptio等。。。。)
- 手把手教你移植 tinyriscv 到FPGA上
- 【剑指Offer】47. 礼物的最大价值
- Discuz small fish game wind shadow legend business gbk+utf8 version template /dz game website template
猜你喜欢

【数字IC/FPGA】检测最后一个匹配序列的位置

Excel print settings public header

捷码赋能案例:湖南天辰产研实力迅速提升!实战玩转智慧楼宇/工地等项目

Golang - the difference between new and make

Vivado FFT IP的使用说明

最新云开发微信余额充电器特效小程序源码

vivado 如何添加时序约束

What problems should be paid attention to in the serpentine wiring of PCB?

Discuz small fish game wind shadow legend business gbk+utf8 version template /dz game website template

vivado VIO IP的用法
随机推荐
Applet referer
小芯片chiplet技术杂谈
[electron] 基础学习
第一性原理(最优解理论)
Typora 1.2.5等版本下载
实践torch.fx:基于Pytorch的模型优化量化神器
The most illusory richest man in China is even more illusory
Technical implementation process of easycvr platform routing log function [code attached]
Ice cream or snow "high"?
How vivado adds timing constraints
MySQL删除表后如何使ID从1开始
用pytorch进行CIFAR-10数据集分类
The latest cloud development wechat balance charger special effect applet source code
C# Winform 读取Resources图片
[network] common request methods
Introduction to quantitative trading
Using xgboost with tidymodels
Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
Detect objects and transfer images through mqtt
c语言之字符串数组