MyBatis框架系列之基础配置(一)

来源:互联网 发布:数据库实体是什么 编辑:程序博客网 时间:2024/06/08 15:14

一、创建maven工程

依赖包的下载 http://mvnrepository.com/

这里写图片描述

二 、创建数据库

因为myBatis是数据库框架,所以我们要再mysql 或者oracle数据库中创建数据库表
我创建的stu作为例子:

实体类:

public class Student {    private Integer id;    private String name;    private String sex;    private String hobby;    public Student() {    }    @Override    public String toString() {        return "Student{" +                "id=" + id +                ", name='" + name + '\'' +                ", sex='" + sex + '\'' +                ", hobby='" + hobby + '\'' +                '}';    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getHobby() {        return hobby;    }    public void setHobby(String hobby) {        this.hobby = hobby;    }}

mapper

public interface StudentMapper {    //1. 创建一个xxxMapper接口文件    List<Student>findAllStudent();    //有参数的batis方法的写法//    Student findStudentById(Integer id);    Student findStudentById(@Param("uid") Integer id);    //增加一条数据    void insertNewStudent(@Param("name") String name, @Param("sex") String sex, @Param("hobby") String hobby);    void insertStu(Student student);    //按照id删除    void deleteByStuId(@Param("uid") Integer id);    //修改    void editByStuId(@Param("uid") Integer id);}

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"><!--针对接口方法的实现都在这个xml文件中写--><mapper namespace="com.wangyingjie.mapper.StudentMapper">    <!--结果映射        作用: 把数据库的列名和实体类的属性名关联起来     -->    <resultMap id="baseMap" type="com.wangyingjie.bean.Student">        <id column="id" property="id" javaType="Integer" jdbcType="INTEGER"/>        <result column="name" property="name" javaType="String" jdbcType="VARCHAR"/>        <result column="sex" property="sex" javaType="String" jdbcType="VARCHAR"/>        <result column="hobby" property="hobby" javaType="String" jdbcType="VARCHAR"/>    </resultMap>    <!--可以封装一段比较固定的sql语句.方便后续的debug-->    <sql id="base_sql">    id, name, sex, hobby  </sql>    <select id="findAllStudent" resultMap="baseMap">        SELECT        <include refid="base_sql"/>        FROM student    </select>    <select id="findStudentById" resultType="Student" parameterType="Integer">    SELECT * FROM student WHERE id = #{uid}  </select>    <insert id="insertNewStudent">    INSERT INTO student VALUES (NULL , #{name}, #{sex}, #{hobby})  </insert>    <insert id="insertStu" useGeneratedKeys="true" keyProperty="id" parameterType="Student">    INSERT INTO student VALUES (NULL , #{name}, #{sex}, #{hobby})  </insert>    <delete id="deleteByStuId" parameterType="Integer">    DELETE  FROM student WHERE id = #{uid}  </delete>    <update id="editByStuId" parameterType="Integer">    UPDATE student SET name = '貂蝉' WHERE id = #{uid}  </update></mapper>

batis-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="db.properties"/>    <!--这是mybatis的配置文件,如果是单独使用Mybatis,必须要这个文件        如果和spring配合,不需要写这个文件        这里面所有的子标签都是有固定顺序的      -->    <typeAliases>        <!--单独对某一个类设置别名-->        <!--<typeAlias type="com.wangyingjie.bean.Student" alias="Student"/>-->        <!--默认一个包 将这个包对应的所有类型的别名设置为类名-->        <package name="com.wangyingjie.bean"/>    </typeAliases>    <environments default="deve">        <!--一个environments标签下可以配置很多个environment标签            使用default属性来确定当前使用的是哪个environment         -->        <environment id="deve">            <transactionManager type="JDBC"/>            <dataSource type="POOLED">                <property name="driver" value="${jdbc_driver}"/>                <property name="url" value="${jdbc_url}"/>                <property name="username" value="${jdbc_username}"/>                <property name="password" value="${jdbc_password}"/>            </dataSource>        </environment>    </environments>  <mappers>      <!--<mapper resource="com/wangyingjie/mapper/StudentMapper.xml" />-->      <!--自动识别设置的包的mapper.xml-->      <package name="com/wangyingjie/mapper"/>  </mappers></configuration>$

db.properties
这里写图片描述

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.0</modelVersion>  <groupId>com.wangyingjie</groupId>  <artifactId>Mybatis_0401_001</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>Mybatis_0401_001 Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis</artifactId>      <version>3.4.2</version>    </dependency>    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.40</version>    </dependency>  </dependencies>  <build>    <finalName>Mybatis_0401_001</finalName>    <resources>      <resource>        <directory>src/main/resources</directory>        <includes>          <include>**/*.properties</include>          <include>**/*.xml</include>          <include>**/*.tld</include>        </includes>        <filtering>false</filtering>      </resource>      <resource>        <directory>src/main/java</directory>        <includes>          <include>**/*.properties</include>          <include>**/*.xml</include>          <include>**/*.tld</include>        </includes>        <filtering>false</filtering>      </resource>    </resources>  </build></project>
原创粉丝点击