mybatis-config.xml 以及遇到的问题

  • MybatisTools 工具类
package com.wei.tools;

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

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

/**
 * mybatis工具类
 * @author 魏小杭
 */
public class MybatisTools {

    static SqlSessionFactory sqlSessionFactory = null;

    static {
        try {
            //使用Mybatis第一步 :获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例.
     *   qlSessionFactory.openSession(true); 的true代表提交事务
     * @return
     */
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}
  • mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>



<!-- 1. 加载jdbc配置文件   优先使用配置文件的内容-->
    <properties resource="jdbc.properties"/>

<!--3.设置日志-->
    <settings>
<!--    标准的日志输出    <setting name="logImpl" value="STDOUT_LOGGING"/>-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

<!-- 2 .起别名-->
    <typeAliases>
<!--        <typeAlias type="com.wei.pojo.User" alias="User"/>-->
<!--        扫描包的方式,在实体类特别多情况下使用,  不要用具体的类名
只是com.wei.pojo 制动识别为pojo实体类的类名小写  比如User  则设别user-->
        <package name="com.wei.pojo"/>
    </typeAliases>



    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>

    </environments>

    <!--    指定要加载的映射文件-->
    <mappers>
        <mapper resource="com/wei/dao/UserMapper.xml"/>
    </mappers>


</configuration>

如果出现Cause: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 5; 1 字节的 UTF-8 序列的字节 1 无效 则:`、

<?xml version="1.0" encoding="UTF8" ?>

  • Maven静态资源导出失败问题,在pom.xml文件添加如下代码
 <!--在build中配置resources,来防止我们资源导出失败的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
  • Mapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--    1.名称空间 用来区分多个不同包下的相同语句 通常是对应的接口包名称-->
<mapper namespace="com.wei.dao.UserMapper">


    <resultMap id="UserMap" type="User">
<!--     解决属性名跟数据库不对应,使用结果集映射
   ccolumn 对应数据库的字段, property 对应实体类的属性
   -->

        <result column="pwd" property="pwd0"/>
    </resultMap>
    
   <select id="getUserById" resultType="user" parameterType="Integer" resultMap="UserMap">
       select * from user where id = #{id}
   </select>

<!--    3.使用模糊查询 "%"#{name}"%" -->
    <select id="selectByName" resultType="com.wei.pojo.Products" parameterType="java.lang.String">

        select * from products where pname like "%"#{name}"%"
    </select>

<!--    4.更新操作-->
    <update id="updateById" parameterType="com.wei.pojo.Products" >
        update products set pname = #{pname},price = #{price},pdate = #{pdate},cid = #{cid} where pid = #{pid}
    </update>

<!--    5. 删除操作-->
    <delete id="delById" parameterType="int" >
        delete from products where pid = #{pid}
    </delete>

<!--    6.插入-->
    <insert id="insertBy" parameterType="com.wei.pojo.Products">
        insert into products value (null,#{pname},#{price},#{pdate},#{cid})
    </insert>




<!--  重点 :增删改一定要提交事务
    select  id="addUser"  对应namespace中的方法名
    resultType  : sql语句的返回值
    parameType ;  参数的类型
      1. parameterType 是什么
            输入的参数类型

    2. resultType 是什么
            是期望的返回结果类型
    3. #{} 用于取值


    5.在CRUD中需要提交事物 sqlSession.commit();     也可以            sqlSession = factory.openSession(true);
-->



</mapper>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值