编写javaweb用到的基本依赖,mybatis-config.xml代码,SqlSessionFactoryUtils.java

Posted yzx-sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写javaweb用到的基本依赖,mybatis-config.xml代码,SqlSessionFactoryUtils.java相关的知识,希望对你有一定的参考价值。

这篇文章仅仅作为记录,供以后复制粘贴使用

pom.xml

    <dependencies>
    <!--Servlet-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>


    <!--jsp-->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
         <version>2.2</version>
        <scope>provided</scope>
    </dependency>

    <!--jstl-->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.5</version>
    </dependency>

    <!--MySQL-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>
    </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>

            </plugin>
        </plugins>
    </build>

mybatis-config.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>

    <typeAliases>
        <package name="com.hailei.pojo对应放置实体类文件夹路径"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///db1?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--扫描映射文件mapper-->
        <package name="com.hailei.mapper对应的mapper文件夹路径"/>
    </mappers>
</configuration>

Util
SqlSessionFactoryUtils.java

package com.hailei.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils 

    private static SqlSessionFactory sqlSessionFactory;

    static 
        //静态代码块会随着类的加载而自动执行,且只执行一次
        try 
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         catch (IOException e) 
            e.printStackTrace();
        
    


    public static SqlSessionFactory getSqlSessionFactory()
        return sqlSessionFactory;
    


先记录这些吧,以后继续添加

[mybatis]逆向工程MGB基本编写

逆向工程




  • 项目结构

  • 依赖

<dependency>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-core</artifactId>
     <version>1.3.7</version>
</dependency>
  • mgb.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- targetRuntime创建一个myBatis容器 有MyBatis3Simple(简单版的CURD)和MyBatis3带有动态sql   -->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <!-- 指定如何连接目标数据库 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"
                        userId="root"
                        password="8888.216">
        </jdbcConnection>
        <!-- 类型解析器 是否强制转换forceBigDecimals -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--javaModelGenerator:指定javaBean的生产策略-->
        <!-- 定义java模型属性  形成javabean   targetPackage目标包-->
        <!--        targetProject 目标工程-->
        <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".\\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 定义sql映射  -->
        <!--sqlMapGenerator sql映射生产策略-->
        <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"
                         targetProject=".\\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- javaClientGenerator 指定Mapper的接口所在的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao"  targetProject=".\\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!--table 指定逆向分析那些表,根据表创建javabean(Departmanet) -->
        <table tableName="tb1_dept" domainObjectName="Department"></table>
        <table tableName="tb1_employee" domainObjectName="Employee"></table>

    </context>
</generatorConfiguration>
  • java
    @Test
    public void mybatisGeneratorTest() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException 

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;

        ConfigurationParser cp = new ConfigurationParser(warnings);
        Reader reader = Resources.getResourceAsReader("mgb.xml");
        Configuration config = cp.parseConfiguration(reader);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);


以上是关于编写javaweb用到的基本依赖,mybatis-config.xml代码,SqlSessionFactoryUtils.java的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb学习——用户登录案例(13)

[mybatis]逆向工程MGB基本编写

002. 使用IDEA创建MyBatis的JAVAWEB项目 ,每一步都有详细过程,完美绕过各种坑能正常运行

Mybatis框架基本使用

导入BaseMapper依赖需要用到的包

MyBatis—案例