myBatis实现读写数据库

一、MyBatis:
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
二、准备:
数据库结构:
这里写图片描述
使用maven添加相应的jar包:


        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>6.0.6version>
        dependency>

项目目录结构:
myBatis实现读写数据库_第1张图片
资源文件的conf.xml配置:



<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/test" />
                <property name="username" value="root" />
                <property name="password" value="mysql" />
            dataSource>
        environment>
    environments>
configuration>

实体类:User

/**   
* @Title: User.java 
* @Package com.king.model 
* @Description: TODO 
* @date 2017年12月25日 上午9:21:57 
* @version V1.0   
*/
package com.king.model;

public class User {

    private int id;
    private String username;
    private String password;
    private int age;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
    }


}

映射文件userMapper.xml



<mapper namespace="com.king.mapping.userMapper">
    

    
    <select id="getUser" parameterType="int" resultType="com.king.model.User">
        select * from user where id=#{id}
    select>
mapper>

然后配置conf.xml将映射加进去:

 <mappers>
        
        <mapper resource="com/king/mapping/userMapper.xml">mapper>
    mappers>

测试类,测试是否能够对数据库进行读写:

package com.king.test;

import java.io.InputStream;

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

import com.king.model.User;

public class Test {

    public static void main(String[] args) {
        String resource="conf.xml";
        InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sessionFactory.openSession();
        String statement = "com.king.mapping.userMapper.getUser";
        User user = session.selectOne(statement,1);
        System.out.println(user);
    }
}

完整代码可以参考:
http://download.csdn.net/download/jsqfengbao/10171913

你可能感兴趣的:(----Java,mybatis,java,持久层框架)