MyBatis笔记——初次条件布置

2.2添加POJO类User.java

package com.pwc.pojo;
public class User {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public User() {
    }
}

1.导包

核心包
图片 1

依赖包
图片 2

理所当然还要有数据库驱动包


2.3添加POJO类User的映照文件User.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.pwc.dao.mapper.UserMapper">
    <select id="findById" parameterType="int" resultType="com.pwc.pojo.User">
        SELECT * FROM User WHERE id = #{id}
    </select>

    <insert id="insert" parameterType="com.pwc.pojo.User">
        INSERT INTO User(name,age) VALUES(#{name},#{age})
    </insert>

    <delete id="deleteById" parameterType="int">
        DELETE * FROM User WHERE id=#{id}

    </delete>

    <select id="findByAge" parameterType="int" resultType="com.pwc.pojo.User">
        SELECT * FROM User WHERE age=#{age}
    </select>

</mapper>

2.写xml配置

花色布局如下
图片 3

2.1添加SqlMapConfig.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/db_pwc" />
                <property name="username" value="pwc" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/pwc/pojo/User.xml" />
    </mappers>
</configuration>

3.测试

package com.pwc.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.pwc.pojo.User;

public class Test {

    public static void main(String[] args) {
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Test.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));

        SqlSession session = factory.openSession();
        User user=session.selectOne("findById", 1);
        System.out.println(user.toString());

    }

}

测试结果如下:
User [id=1, name=pwc, age=18]

版权讲明:本文为博主原创作品,未经博主允许不得转载。

 

简言之介绍

MyBatis 本是apache的一个开源项目iBatis, 二〇一〇年这些体系由apache software
foundation 迁移到了google code,并且改名为MyBatis
。二零一三年12月动迁到Github。
iBATIS一词来源于“internet”和“abatis”的重组,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL
Maps和Data Access Objects(DAO)

图片 4
图片 5


网站地图xml地图