Mybatis常用文本

1、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>
    <properties resource="jdbc.properties"></properties>

    <typeAliases>
        <typeAlias type="com.blb.dto.TUser" alias="user"></typeAlias><!--设置单个别名-->
        <package name="com.blb.dto"/><!--给包下所有类起别名,默认别名为首字母小写-->
    </typeAliases>


    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <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>
    <!--引入带sql的mapper文件-->
    <mappers>
        <mapper resource="com/blb/mapper/User-Mapper.xml"></mapper>
        <mapper resource="com/blb/mapper/Type-Mapper.xml"></mapper>
    </mappers>

</configuration>

2、jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:6666/haha
username=root
password=123456


mssql.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
mssql.url=jdbc:sqlserver://localhost:1433;DatabaseName=haha
mssql.username=sa
mssql.password=1234

3、mapper.xml与mapper类

User-Mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.blb.mapper.TUserMapper">

    <select id="selectUserById" parameterType="integer" resultType="uuser">
        select * from t_user where id=#{sid}
    </select>
    <!--把插入的数据自增的主键传给id,若insert加keyProperty则会被selectKey覆盖-->
    <insert id="addUser" parameterType="user" >
        <selectKey keyProperty="id" order="AFTER" resultType="int">
            select last_insert_id()
        </selectKey>
        insert into t_user(username,password) values (#{username},#{password})
    </insert>



    <!--把插入的数据自增的主键传给id-->
    <insert id="insertUser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
       <!--把不主键自增的值传给id
        <selectKey keyProperty="id" order="BEFORE" resultType="integer">
            select max(id)+1 from t_user
        </selectKey>
        -->
        insert into t_user(username,password) values (#{username},#{password})
    </insert>
    <delete id="deleteUser" parameterType="integer">
        delete from t_user where id=#{sid}
    </delete>
    <update id="updateById" parameterType="user">
        update t_user set username=#{username},password=#{password} where id=#{id}
    </update>
    <select id="selectUserByPage" parameterType="map" resultType="user">
        select * from t_user limit #{start},#{end}
    </select>
    <select id="selectCount" resultType="java.lang.Long">
        select count(1) from t_user
    </select>
</mapper>

TUserMapper类:

package com.blb.mapper;

import com.blb.dto.TUser;

import java.util.List;
import java.util.Map;

public interface TUserMapper {
    public void insertUser(TUser user);

    public void deleteUser(int id);

    public void updateById(TUser user);

    public TUser selectUserById(int id);

    public List<TUser> selectUserByPage(Map<String,Object> param);

    public long selectCount();

}

Type-Mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.blb.mapper.TTypeMapper">
    <!--表的字段映射到想类的属性-->
    <resultMap id="type" type="tType">
        <id property="tId" column="t_id"></id>
        <result property="tName" column="t_name"></result>
        <result property="pid" column="pid"></result>
        <result property="tDesc" column="t_desc"></result>
     <!--单个-->
<association property="tUser" select="queryTypeBySid" column="sid"></association>

     <!--集联多个,根据pid=t_id用到queryTypeByPid再查一次返回types对象,queryTypeByPid方法本身也会映射给type-->  <collection property="types" select="com.blb.mapper.TTypeMapper.queryTypeByPid" column="t_id"> </collection> </resultMap> <select id="queryTypeBySid" parameterType="int" resultType="tUser"> select * from t_type where sid=#{sid} </select> <!--先查出tid=0的信息,映射给上面type--> <select id="queryTypeByTid" parameterType="int" resultMap="type"> select * from t_type where t_id=#{tid} </select>    <select id="queryTypeByPid" parameterType="int" resultMap="type"> select * from t_type where pid=#{pid} </select> </mapper>

TypeMapper:

package com.blb.mapper;

import com.blb.dto.TType;

import java.util.List;

public interface TTypeMapper {
    public List<TType> queryTypeByPid(int pid);
    public TType queryTypeByTid(int tid);

}

4、pom文件

<?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>com.blb</groupId>
  <artifactId>mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>mybatis</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.8</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.sqlserver</groupId>
      <artifactId>sqljdbc4</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>







    <dependency>
      <groupId>commons-dbutils</groupId>
      <artifactId>commons-dbutils</artifactId>
      <version>1.6</version>
    </dependency>
  </dependencies>

  <build>
    <!--把java文件下所有的.xml文件加载-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>

    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
原文地址:https://www.cnblogs.com/asksk/p/12634701.html