I.はじめに
1.これは、単に春とMybatisPlusの統合であり、最も簡単なテスト製の
最初の春のフレームワークに精通していると、すべての、春とMybatisPlusを統合するには2.
3. Mavenのプロジェクトのビルドツール
第二に、ステップ
1.どのように単純なディレクトリ構造を示し、プロジェクトが省略されて作成します。。。
POMファイルでまず追加の依存関係
<dependencies>
<!-- 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.6</version>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
知っておくべき3学んだMyBatisのは、グローバルコンフィギュレーションファイルがあるMyBatisのが、コンテンツを追加した後、春の内側との統合を必要としませんが、我々は、一般的に予約を持っています。リソースディレクトリを構築するmybatis-config.xml
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>
コアファイルを構築するための4.resourceの春カタログの下ではapplicationContext.xml
、このxmlファイル。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">
<!--引入properties配置文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--配置数据源,使用C3p0连接池-->
<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"/>
<!--整合Mybatis-->
<!--Spring一启动就会创建sqlSessionFactory对象-->
<!--
mybatis提供的:org.mybatis.spring.SqlSessionFactoryBean
mybatisPlus 提供的:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
5.resource下のディレクトリを構築するdb.proerties
プロパティファイル
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis_plus?characterEncoding=utf-8
jdbc.username=root
jdbc.password=自己数据的密码
6.私たちのテストを容易にするために、ログファイルを追加します。下のリソースディレクトリを構築しlog4j.xml
、この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>
テストに7として、我々は、データベーステーブルとエンティティクラスを準備する必要があり
、(1)データベースのテーブルをMySQLデータベースを使用して、新しいデータベースとテーブルを作成し、いくつかのデータを追加します。
CREATE DATABASE db_mybatis_plus;
USE db_mybatis_plus;
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50), email VARCHAR(50),
gender INT(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('rose','[email protected]',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('jack','[email protected]',0,35);
(2)は、Javaエンティティクラス、データベーステーブルのフィールド。
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Integer age;
//无参构造
//有参构造
//getter和setter方法
//toString 方法
//太占地了,我就省略l,但是你不要省略,特别的setter和getter方法。
(3)MybatisPlusが設けBaseMapperインターフェイスを言及継承、マッパーインターフェイスを作成する、一般的なエンティティ・クラス・オブジェクト
EmployeeMapper
public interface EmployeeMapper extends BaseMapper<Employee>{
}
写真を来、心配しないでください
第三に、テスト
1.最初のテストは、データベース接続が成功しません
//获取Spring容器
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper",EmployeeMapper.class);
テストメソッドを書きます
@Test
public void testMp() throws SQLException {
DataSource dataSource = ioc.getBean("dataSource",DataSource.class);
System.out.println(dataSource);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
張マップ、エラーなし、おそらく構成に問題なしに
、ログ効果
はもちろん、使用するテスト挿入方法挿入2.ので、おそらくテストに直接文句を言うでしょう
。3.エラーメッセージ
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.atguigu.mp.beans.Employee' with value '1237661424084324353' Cause: java.lang.IllegalArgumentException: argument type mismatch
4.ソリューションは、エンティティタイプのidプロパティに注釈を追加し、自己宣言の主キーの成長戦略であります
@TableId(type = IdType.AUTO)
5.実行して、再び例外がスローされます
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mp.employee' doesn't exist
クラス名を注釈付きエンティティクラスでは、エンティティクラスの宣言の対応するデータベーステーブルへのソリューション、(これは例外をスローしていない場合、彼らは、エンティティクラスとテーブル名と一致しません)
@TableName(value = "tbl_employee")
2つの例外は、当然のことながら、ここでは言及しないように、簡単に解決策があります
、それは1の値を返す6.実行時に、これが成功すると、SQL文のプリントアウト
あなたが行われているが
書き込みは容易ではない、あなたに役立つならば、あなたに感謝し、私にとって大きな支援と励ましを少し賞賛、親指を与えてください!!!