目录
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>ideamavenartifactId>
- <groupId>org.examplegroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>mybatis01artifactId>
- <packaging>warpackaging>
-
- <name>mybatis01 Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>4.0.0version>
- <scope>providedscope>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.4.5version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
-
-
-
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>2.9.1version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>2.9.1version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>2.9.1version>
- dependency>
- dependencies>
-
-
- <build>
- <finalName>mybatis01finalName>
- <resources>
-
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
-
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>jdbc.propertiesinclude>
- <include>*.xmlinclude>
- includes>
- resource>
- resources>
-
- <pluginManagement>
- <plugins>
-
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.44version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- plugin>
-
-
-
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>
补全maven项目的目录,如图:
resources中jdbc.properties 数据库连接配置
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8
- jdbc.username=root
- jdbc.password=123456
安装以下插件
Free mybatis plugin
Mybatis generator
mybatis tools
maven helper
详细介绍
Intellij Idea Mybatis 插件(plugin和tools插件)_yangshijin1988的博客-CSDN博客_idea mapper 插件
下载maven helper之后 可以看到配置文件中所用的依赖
修改web.xml
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- web-app>
-
mybatis.cfg.xml
- configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
- <properties resource="jdbc.properties"/>
-
- <settings>
- <setting name="logImpl" value="LOG4J2"/>
- settings>
-
-
- <typeAliases>
-
- typeAliases>
-
-
-
- <environments default="development">
- <environment id="development">
-
- <transactionManager type="jdbc"/>
-
-
-
-
-
- <dataSource type="POOLED">
- <property name="driver"
- value="${jdbc.driver}"/>
- <property name="url"
- value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- dataSource>
- environment>
- environments>
-
- <mappers>
-
- mappers>
- configuration>
基于ssm的逆向工程使用
- 安装Mybatis generator插件 已下载安装好
- 配置generatorConfig.xml
- 配置maven运行generator命令
- 在pom中处理generatorConfig.xml不能编译问题
generatorConfig.xml
- generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
- <generatorConfiguration>
-
- <properties resource="jdbc.properties"/>
-
-
- <classPathEntry location="E:\\cdl_mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
-
-
- <context id="infoGuardian">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true"/>
- <property name="suppressDate" value="true"/>
- commentGenerator>
-
-
- <jdbcConnection driverClass="${jdbc.driver}"
- connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
-
-
- <javaTypeResolver>
-
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
-
-
-
- <javaModelGenerator targetPackage="com.cdl.model"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
-
- <property name="constructorBased" value="true"/>
-
- <property name="trimStrings" value="false"/>
-
- <property name="immutable" value="false"/>
- javaModelGenerator>
-
-
- <sqlMapGenerator targetPackage="com.cdl.mapper"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
- sqlMapGenerator>
-
-
-
-
-
- <javaClientGenerator targetPackage="com.cdl.mapper"
- targetProject="src/main/java" type="XMLMAPPER">
-
- <property name="enableSubPackages" value="false"/>
- javaClientGenerator>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <table schema="" tableName="t_mvc_Book" domainObjectName="Book"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
-
- table>
-
-
- <table schema="" tableName="t_oa_permission" domainObjectName="Permission"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- table>
-
- context>
- generatorConfiguration>
写命令
写入命令
结果
写入命令后运行会自动将数据库的表、字段名 映射成对应的类名和属性名
并且生成增删改查的方法
总结一下:
1.添加配置文件
1.1 修改5.1.44的jar的路径
1.2 修改实体类生成的地址
1.3 修改SQL对应的配置文件的生成地址
1.4修改Dao层代码地址
1.5指定需要生成增删改查代码对应的表
2.配置maven的mybatis逆向生成代码的命令
commond line
3.在pom文件中使用配置好的命令
sqlsession的作用:
1.sqlsession可以拿到mapper对象
2.作为缓存使用,一级缓存,默认会开启的缓存、
3.出于对性能的考虑,会采用二级缓存,二级缓存需要手动开启
测试逆向生成的代码是否正确
有事务的:增删改
无事务的:查询
只需要测 查询和删除 即可知道是否正确
建一个com.cdl.biz.impl的包
- package com.cdl.biz;
-
- import com.cdl.model.Book;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:00
- */
- public interface BookBiz {
-
- int deleteByPrimaryKey(Integer bid);
-
-
- Book selectByPrimaryKey(Integer bid);
-
-
- }
com.cdl.util
SessionUtil
- package com.cdl.util;
-
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:47
- */
- public class SessionUtil {
- private static SqlSessionFactory sessionFactory;
- private static ThreadLocal
threadLocal = new ThreadLocal(); - static {
- sessionFactory = new SqlSessionFactoryBuilder().build(SessionUtil.class.getResourceAsStream("/mybatis.cfg.xml"));
- }
-
- public static SqlSession openSession() {
- SqlSession session = threadLocal.get();
- if (null == session) {
- session = sessionFactory.openSession();
- threadLocal.set(session);
- }
- return session;
- }
-
- public static void main(String[] args) {
- SqlSession session = openSession();
- System.out.println(session.getConnection());
- session.close();
- // System.out.println(session.getConnection());
- }
- }
//alt + insert 快速提供set/get/tostring/构造函数 //alt +enter 快速构建实现类 能够自动补全
test类:
- package com.cdl.biz.impl;
-
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import static org.junit.Assert.*;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:55
- */
- public class BookBizImplTest {
-
- @Before
- public void setUp() throws Exception {
- System.out.println("初始换方法。。。");
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("方法测试结束。。");
- }
-
- @Test
- public void deleteByPrimaryKey() {
- }
-
- @Test
- public void selectByPrimaryKey() {
- System.out.println("测试的业务方法。。。");
- }
-
- }
运行结果:
修改配置文件 mybatis.cfg.xml
测试类:
- package com.cdl.biz.impl;
-
- import com.cdl.mapper.BookMapper;
- import com.cdl.util.SessionUtil;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:55
- */
- public class BookBizImplTest {
-
- private BookBizImpl bookBiz;
- SqlSession sqlSession;
-
- @Before
- public void setUp() throws Exception {
- System.out.println("初始换方法。。。");
- BookBizImpl bookBiz = new BookBizImpl();
- //工具类中获取session对象
- sqlSession = SessionUtil.openSession();
- //从session对象中获取mapper对象
- BookMapper mapper = sqlSession.getMapper(BookMapper.class);
- bookBiz.setBookMapper(mapper);
- this.bookBiz = bookBiz;
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("方法测试结束。。");
- }
-
- @Test
- public void deleteByPrimaryKey() {
- }
-
- @Test
- public void selectByPrimaryKey() {
-
- System.out.println("测试的业务方法。。。");
- //System.out.println(bookBiz.getBookMapper());
- System.out.println(bookBiz.selectByPrimaryKey(44));
- }
-
- }