通过Mybatis实现对单表的增删改查-通过定义一个接口实现

Pom.xml文件 定义工程项目依赖的软件库

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>org.vincentgroupId>
  <artifactId>mybatiswebartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>mybatisweb Maven Webappname>
  <url>http://maven.apache.orgurl>
  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.10version>
      <scope>testscope>
    dependency>

    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.4.4version>
    dependency>

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.42version>
    dependency>

  dependencies>
  <build>
  
    <finalName>mybatiswebfinalName>
    <plugins>
        <plugin>
            
            <groupId>org.codehaus.cargogroupId>
            <artifactId>cargo-maven2-pluginartifactId>
            <version>1.4.12version> 
            <configuration>
                <container>
                    
                    <containerId>tomcat8xcontainerId>
                     
                    <home>C:\fastDev\Tomcat\apache-tomcat-8.5.14home>                 
                container>
              <configuration>
                <type>existingtype>
                 
                <home>C:\fastDev\Tomcat\apache-tomcat-8.5.14home>
                <properties><cargo.servlet.port>9090cargo.servlet.port>properties>
              configuration>            
            configuration>
            <executions>
                <execution>
                    <id>cargo-runid>
                    <phase>installphase>
                    <goals>
                        <goal>rungoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
  build>
project>

src/main/resources目录下放置了mybatis的所有配置文件该目录下文件可以直接被java代码引用

为了管理,在src/main/resources目录下新建mybatis目录管理所有mybatis相关的配置信息。

mybatis配置数据源信息

MybatisConfig.xml 在src/main/resources/mybatis目录

"1.0" encoding="UTF-8"?>

"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
        "org.vincent.model.Student"
        alias="Student"/>
    
    default="development">
        "development">
            
            "JDBC" />
            
            "POOLED">
                "driver" value="com.mysql.jdbc.Driver" />
                "url" value="jdbc:mysql://localhost:3306/javaee" />
                "username" value="root" />
                "password" value="1557862201" />
            
        

      
    
     
        "mybatis/mappers/Student.xml"/>
    

设置项目中POJO类和数据库表的映射关系

Student.xml




<mapper namespace="org.vincent.model.IStudentOperation">
    
    
    <select id="getStudentbyId" parameterType="int" 
        resultType="Student"> 
        select * from student where id=#{id}
    select>

     
    <select id="getStudents" parameterType="int"
        resultMap="list">
            select * from student where id >#{id}
     select>
     
     <resultMap type="Student" id="list">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="address" property="address"/>
     resultMap>

     
     <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO student(name, age, address) VALUES(#{name}, #{age} ,#{address} )
     insert>
     
     <update id="updateStudent" parameterType="Student" >
        update student SET name=#{name} , address=#{address} ,age=#{age} where id=#{id}
     update>

     <delete id="deleteStudent" parameterType="int">
        delete from student where id =#{id}
     delete>

mapper>

POJO 类

package org.vincent.model;

/**
 * 数据库表对应的Java类;POJO对象
 *
 * @ClassName: Student
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author PengRong
 * @date 2017年6月16日 上午12:35:37
 *
 */
public class Student {
    private int id;
    private String address;
    private String name;
    private int age;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [id=" + this.id + ", address=" + this.address
                + ", name=" + this.name + ", age=" + this.age + "]";
    }

}

接口类

package org.vincent.model;

import java.util.List;

/**
 * 只有接口没有实现类,用于Mybatis中查询结果 这里的接口方法名必须和Student.xml中select中id属性一致;
 * 比如getStudentbyId 必须和