Mybatis环境搭建

Mybatis环境搭建

      • 创建工程
      • 文件目录
      • pom.xml文件
      • User类
      • IUserDao类
      • 主配置文件
      • Dao接口的映射文件
      • 测试类
      • 采用注解形式实现数据库操作
        • 文件目录
      • 测试结果

创建工程

首先 需要创建一个新建工程,选择Maven工程
Mybatis环境搭建_第1张图片

文件目录

Mybatis环境搭建_第2张图片

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.jjp.mybatisgroupId>
    <artifactId>mybatis_demoartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.12version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.10version>
        dependency>
    dependencies>
project>


User类

User类是与数据库中指定数据表的字段一一对应的实体类。
User类如下

package com.jjp.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;


    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

IUserDao类

IUserDao类是实现数据库“查询所有”操作的接口

package com.jjp.dao;

import com.jjp.domain.User;

import java.util.List;

public interface IUserDao {

    /*
    *查询所有操作
    */
    List<User> findAll();
}

主配置文件

SqlMapConfig.xml




<configuration>
    <properties>
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/ssmdemo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT" />
        <property name="username" value="root" />
        <property name="password" value="J123" />
    properties>
    <typeAliases>
        <typeAlias alias="role" type="com.jjp.domain.User" />
    typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <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>
    <mappers>
        <mapper resource="com\jjp\dao\IUserDao.xml" />
    mappers>
configuration>

Dao接口的映射文件

IUserDao.xml



<mapper namespace="com.jjp.dao.IUserDao">
    
    <select id="findAll" resultType="com.jjp.domain.User">
        select * from user
    select>
mapper>

测试类

MybatisTest.java

package com.jjp.test;

import com.jjp.dao.IUserDao;
import com.jjp.domain.User;
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.InputStream;
import java.util.List;
public class MybatisTest {
    /**
     * 入门案例
     * @param args
     */
    public static void main(String[] args)throws Exception {
        //1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3.使用工厂生产SqlSession对象
        SqlSession session = factory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IUserDao userDao = session.getMapper(IUserDao.class);
        //5.使用代理对象执行方法
        List<User> users = userDao.findAll();
        for(User user : users){
            System.out.println(user);
        }
        //6.释放资源
        session.close();
        in.close();
    }
}

采用注解形式实现数据库操作

文件目录

Mybatis环境搭建_第3张图片
发现与上个文件夹相比,少了Dao映射文件夹
然后我们在原有Dao接口文件里面填上一句注解
@Select(“select * from user”)

package com.jjp.dao;

import com.jjp.domain.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IUserDao {

    /*
    *查询所有操作
    */
    @Select("select * from user")
    List<User> findAll();
}

在主配置文件SqlMapConfig.xml中 将Mappers层的mapper更改成下面这样

 <mappers>
        <mapper class="com.jjp.dao.IUserDao" />
 mappers>

这样也能实现查询所有的操作

测试结果

Mybatis环境搭建_第4张图片

你可能感兴趣的:(JAVA)