MyBatis Simple Using

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis Simple Using相关的知识,希望对你有一定的参考价值。

什么是 MyBatis ?

MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

MyBastis是个半自动化的ORM工具,其在配置文件XML里面进行SQL的拼写,当然与实体框架entityframework是有很大的区别。

先用 eclipse建立一个java项目、数据库使用mysql、引用MyBastis外部是3.4.1

技术分享

首先进行实体的建立

package model;

public class student {
    
    public String name,address;
    public int age;
    public student()
    {
      //构造函数    
    }
    
    public String getname()
    {
        return this.name;
    }
    
    public void setname(String name)
    {
        this.name=name;
    }
    
    public String getaddress()
    {
        return this.address;
    }
    
    public void setaddress(String address)
    {
        this.address=address;
    }
    
    public int getage()
    {
        return this.age;
    }
    
    public void setage(int age)
    {
        this.age=age;
    }

}

然后在建立根目录的Config的配置

<?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>  
    <typeAliases>  
        <typeAlias type="model.student" alias="student" />  
    </typeAliases>  
    <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/school?useSSL=true" />
                <property name="username" value="root" />  
                <property name="password" value="123456" />  
            </dataSource>  
        </environment>  
    </environments>  
    <mappers>  
        <mapper resource="mapper/mappers.xml" />  
    </mappers>  
</configuration> 

再然后进行mapper的建立

<?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="dao.StudentMapper">  
    <select id="selectsudent" parameterType="String" resultType="student">  
        select name,age,address from student where name=#{name}
    </select>
    <insert id="insertstudent" parameterType="student">
       insert into student(name,age,address) value(#{name},#{age},#{address})
    </insert>  
</mapper>  

在定义相关接口

package dao;

import model.student;

public interface StudentMapper {
    /*
     * 返回一个model
     */
    public student selectsudent(String Id);
    
    /*
     * 插入一条数据
     */
    public void insertstudent(student model);
    
}
package mybastis;

import java.io.IOException;

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 dao.StudentMapper;
import model.student;

public class studentsmain {

     private static SqlSessionFactory getSessionFactory() {  
            SqlSessionFactory sessionFactory = null;  
            String resource = "Config.xml";  
            try {  
                sessionFactory = new SqlSessionFactoryBuilder().build(Resources  
                        .getResourceAsReader(resource));  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            return sessionFactory;  
        }  
    
    public static void main(String[] args) {
            SqlSession sqlSession = getSessionFactory().openSession();  
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);        
            student user = studentMapper.selectsudent("dddd");      
            System.out.println(user.getaddress()+user.getage()+user.getname
    }
}

未完待续。。。。。

MyBatis官网: http://www.mybatis.org/mybatis-3/zh/

 

以上是关于MyBatis Simple Using的主要内容,如果未能解决你的问题,请参考以下文章

Simple live system using nginx

连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段

[simple-orm-mybaits]基于Mybatis的ORM封装介绍

七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.(代码片段

a simple java login using jsp, servlet, javabean

Rapid Object Detection using a Boosted Cascade of Simple Features