前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >​Mybatis 各个文件的配置

​Mybatis 各个文件的配置

原创
作者头像
花落花相惜
修改2021-11-20 15:58:51
2250
修改2021-11-20 15:58:51
举报

1.mybatis-config.xml

代码语言:txt
复制
<?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>
    <!--导入外部资源路径 -->
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="databases/AdministratorMapper.xml" />
        <mapper resource="databases/CarMessagesMapper.xml" />
        <mapper resource="databases/InOutMapper.xml" />
        <mapper resource="databases/ParkMessagesMapper.xml" />
    </mappers>
</configuration>]

2.pom.xml

代码语言:txt
复制
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
    <artifactId>MybatisStudy</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mybatis01</module>
    </modules>
<dependencies>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
</dependencies>
</project>

3.MybatisUtils

代码语言:txt
复制
package mybatis.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * description:  <br>
 * date: 2021/4/26 20:59 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
public class MybatisUtils {<!-- -->
    private static SqlSessionFactory sqlSessionFactory;
    static {<!-- -->
        //使用mybatis第一步,获取sqlSessionFactory对象
        String resources = "mybatis-config.xml";
        try {<!-- -->
            InputStream inputStream = Resources.getResourceAsStream(resources);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
    }
    //既然有了sqlSessionFactory,顾名思义,我们就可以从中获取SqlSession的实例
    //SqlSession 完全包含了面向数据库执行sql的所有方法
    public static SqlSession getSqlSession(){<!-- -->
        return sqlSessionFactory.openSession();
    }
}

4.db.properties

代码语言:txt
复制
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisuseSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
// 你的用户名
username=root
// 你的密码
password=123456

5.UserMapper.xml(Mapper例子)

代码语言:txt
复制
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;
&lt;mapper namespace="com.tansty.dao.UserMapper"&gt;
    &lt;select id="getUserList" resultType="com.tansty.pojo.User"&gt;
        select * from mybatis.user
    &lt;/select&gt;
    &lt;select id="getUserList2" resultType="com.tansty.pojo.User" &gt;
        select * from mybatis.user where user like #{value}
    &lt;/select&gt;
&lt;insert id="insertUser" parameterType="com.tansty.pojo.User" &gt;
        insert into mybatis.user(id,user,pwd) values(#{id},#{user},#{pwd})
    &lt;/insert&gt;
&lt;insert id="insertUser2" parameterType="map" &gt;
        insert into mybatis.user(id,user,pwd) values(#{id},#{userName},#{password})
    &lt;/insert&gt;
&lt;delete id="deleteUser" parameterType="int" &gt;
        delete from mybatis.user where id=#{id}
    &lt;/delete&gt;
&lt;update id="updateUser" parameterType="com.tansty.pojo.User"&gt;
        update mybatis.user set user = #{user},pwd=#{pwd} where id=#{id} ;
    &lt;/update&gt;
&lt;/mapper&gt;

6.资源导出问题解决

代码语言:txt
复制
 &lt;build&gt;
        &lt;resources&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*.properties&lt;/include&gt;
                    &lt;include&gt;**/*.xml&lt;/include&gt;
                &lt;/includes&gt;
            &lt;/resource&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/java&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*.properties&lt;/include&gt;
                    &lt;include&gt;**/*.xml&lt;/include&gt;
                &lt;/includes&gt;
            &lt;/resource&gt;
        &lt;/resources&gt;
    &lt;/build&gt;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.mybatis-config.xml
  • 2.pom.xml
  • 3.MybatisUtils
  • 4.db.properties
  • 5.UserMapper.xml(Mapper例子)
  • 6.资源导出问题解决
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档