IntelliJ IDEA 2018版 Mybatis-Plus学习笔记(二)

 第二章    使用实例
 
1.搭建测试数据库
 1 -- 创建库
 2 CREATE DATABASE mp;
 3 -- 使用库
 4 USE mp;
 5 -- 创建表
 6 CREATE TABLE tbl_employee(
 7 id INT(11) PRIMARY KEY AUTO_INCREMENT, last_name VARCHAR(50),
 8 email VARCHAR(50),
 9 gender CHAR(1),
10 age int );
11 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);
View Code

2.搭建成功后

    3. 项目搭建

       建立一个maven项目

            设置包名和项目名称

      确认成功

           新建包名

4.实体类创建

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

源码:

 1 package com.baidu.www.mplus;
 2 
 3 
 4 /**
 5  *
 6  * @author liuyangos8888
 7  *
 8  * 实体类
 9  *
10  */
11 public class Employee {
12 
13     /**
14      * 字段的ID
15      */
16     private Integer id ;
17 
18     /**
19      * 名字
20      */
21     private String lastName;
22 
23     /**
24      * 邮箱
25      */
26     private String email ;
27 
28     /**
29      * 性别
30      */
31     private Integer gender ;
32 
33     /**
34      * 年龄
35      */
36     private Integer age ;
37 
38 
39     public Integer getId() {
40         return id;
41     }
42 
43     public void setId(Integer id) {
44         this.id = id;
45     }
46 
47     public String getLastName() {
48         return lastName;
49     }
50 
51     public void setLastName(String lastName) {
52         this.lastName = lastName;
53     }
54 
55     public String getEmail() {
56         return email;
57     }
58 
59     public void setEmail(String email) {
60         this.email = email;
61     }
62 
63     public Integer getGender() {
64         return gender;
65     }
66 
67     public void setGender(Integer gender) {
68         this.gender = gender;
69     }
70 
71     public Integer getAge() {
72         return age;
73     }
74 
75     public void setAge(Integer age) {
76         this.age = age;
77     }
78 }
View Code

5.SSM基础环境配置搭建

    (1)全局mybaits-config.xml

1 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration
2         PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
3 <configuration>
4 
5 
6 </configuration>
View Code

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

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 3 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 4     <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
 5         <param name="Encoding" value="UTF-8"/>
 6         <layout class="org.apache.log4j.PatternLayout">
 7             <param name="ConversionPattern" value="%-5p
 8 %d{HH:mm :ss,SSS} %m (%F:%L) \n"/>
 9         </layout>
10     </appender>
11     <logger name="java.sql">
12         <level value="debug"/>
13     </logger>
14     <logger name="org.apache.ibatis">
15         <level value="info"/>
16     </logger>
17     <root>
18         <level value="debug"/>
19         <appender-ref ref="STDOUT"/>
20     </root>
21 </log4j:configuration>
View Code

   (3)数据库配置db.properties

1 jdbc.driver=com.mysql.jdbc.Driver 
2 jdbc.url=jdbc:mysql://localhost:3306/mp
3 jdbc.username=root
4 jdbc.password=123456
View Code

     (4) applicationContext.xml组合配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 7        xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
 8     http://mybatis.org/schema/mybatis-spring-1.2.xsd
 9     http://www.springframework.org/schema/beans
10     http://www.springframework.org/schema/beans/spring-beans.xsd
11     http://www.springframework.org/schema/context
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
15     <!-- 数据源 -->
16     <context:property-placeholder location="classpath:db.properties"/>
17 
18     <bean id="dataSource"
19           class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="driverClass" value="${jdbc.driver}"></property>
21         <property name="jdbcUrl" value="${jdbc.url}"></property>
22         <property name="user" value="${jdbc.username}"></property>
23         <property name="password" value="${jdbc.password}"></property>
24     </bean>
25 
26     <!-- 事务管理器 -->
27     <bean id="dataSourceTransactionManager"
28           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
29         <property name="dataSource" ref="dataSource"></property>
30     </bean>
31 
32     <!-- 基于注解的事务管理 -->
33     <tx:annotation-driven
34             transaction-manager="dataSourceTransactionManager"/>
35 
36 
37     <!-- 配置 SqlSessionFactoryBean -->
38     <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
39 
40         <!-- 数据源 -->
41         <property name="dataSource" ref="dataSource"></property>
42 
43         <property name="configLocation" value="classpath:mybatis-config.xml">
44 
45         </property>
46 
47         <!-- 别名处理 -->
48         <property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">
49         </property>
50     </bean>
51     
52     <!--配置 mybatis 扫描 mapper 接口的路径 -->
53     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
54         <property name="basePackage"
55                   value="com.baidu.www.mplus.mapper">
56 
57         </property>
58     </bean>
59 </beans>
View Code

    (5)目录结构

6.测试类编写

 1 import org.junit.Test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 
 6 import javax.sql.DataSource;
 7 import java.sql.Connection;
 8 import java.sql.SQLException;
 9 
10 
11 /**
12  * test
13  */
14 public class TestMp {
15     private ApplicationContext iocContext = new ClassPathXmlApplicationContext("applicationContext.xml");
16 
17 
18     @Test
19     public void context() throws SQLException {
20 
21         DataSource ds = iocContext.getBean("dataSource", DataSource.class);
22         Connection conn = ds.getConnection();
23         System.out.println(conn);
24 
25     }
26 }
View Code

7.Mybatisplus替代SqlSessionFactory,修改 applicationContext.xml组合配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 7        xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
 8     http://mybatis.org/schema/mybatis-spring-1.2.xsd
 9     http://www.springframework.org/schema/beans
10     http://www.springframework.org/schema/beans/spring-beans.xsd
11     http://www.springframework.org/schema/context
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
15     <!-- 数据源 -->
16     <context:property-placeholder location="classpath:db.properties"/>
17 
18     <bean id="dataSource"
19           class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="driverClass" value="${jdbc.driver}"></property>
21         <property name="jdbcUrl" value="${jdbc.url}"></property>
22         <property name="user" value="${jdbc.username}"></property>
23         <property name="password" value="${jdbc.password}"></property>
24     </bean>
25 
26     <!-- 事务管理器 -->
27     <bean id="dataSourceTransactionManager"
28           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
29         <property name="dataSource" ref="dataSource"></property>
30     </bean>
31 
32     <!-- 基于注解的事务管理 -->
33     <tx:annotation-driven
34             transaction-manager="dataSourceTransactionManager"/>
35 
36 
37     <!-- 配置 SqlSessionFactoryBean -->
38     <!--<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">-->
39 
40         <!--&lt;!&ndash; 数据源 &ndash;&gt;-->
41         <!--<property name="dataSource" ref="dataSource"></property>-->
42 
43         <!--<property name="configLocation" value="classpath:mybatis-config.xml">-->
44 
45         <!--</property>-->
46 
47         <!--&lt;!&ndash; 别名处理 &ndash;&gt;-->
48         <!--<property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">-->
49         <!--</property>-->
50     <!--</bean>-->
51 
52     <!--&lt;!&ndash;修改为MybatisPlus配置&ndash;&gt;-->
53     <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
54 
55         <!-- 数据源 -->
56         <property name="dataSource" ref="dataSource"></property>
57 
58         <property name="configLocation" value="classpath:mybatis-config.xml">
59 
60         </property>
61 
62         <!-- 别名处理 -->
63         <property name="typeAliasesPackage" value="com.baidu.www.mplus.bean">
64         </property>
65     </bean>
66 
67 
68 
69     <!--配置 mybatis 扫描 mapper 接口的路径 -->
70     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
71         <property name="basePackage"
72                   value="com.baidu.www.mplus.mapper">
73 
74         </property>
75     </bean>
76 </beans>
View Code

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

猜你喜欢

转载自www.cnblogs.com/liuyangfirst/p/9721688.html