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

DOCTYPE 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="LOG4J"/>
    settings>


    <typeAliases>


        <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 无效 则:`、



  • Maven静态资源导出失败问题,在pom.xml文件添加如下代码
 
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>
  • Mapper.xml

DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.wei.dao.UserMapper">


    <resultMap id="UserMap" type="User">


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


    <select id="selectByName" resultType="com.wei.pojo.Products" parameterType="java.lang.String">

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


    <update id="updateById" parameterType="com.wei.pojo.Products" >
        update products set pname = #{pname},price = #{price},pdate = #{pdate},cid = #{cid} where pid = #{pid}
    update>


    <delete id="delById" parameterType="int" >
        delete from products where pid = #{pid}
    delete>


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








mapper>



你可能感兴趣的:(mybatis,java,xml,ssm)