mybatis 简单的入门实例

第一步:添加mybaties的架包

 

第二步:配置mybaties的文件

<?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="jdbc.properties"/>
	<typeAliases>
		<typeAlias alias="Student" type="com.java.entity.Student"/>
	</typeAliases>
	<!--开发环境-->
	<environments default="development">
		<environment id="development">
			<!--配置数据源-->
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!--扫描mybatis的实现-->
		<mapper resource="com/java/mapper/StudentMapper.xml" />
	</mappers>
</configuration>

  

第三步:

编写实体、mapper的接口以及SqlSessionFactotyUtil工厂

package com.java.entity;

/**
 * @author nidegui
 * @create 2019-06-25 14:25
 */
public class Student {
    private Integer id;
    private String name;
    private String password;

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "entity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public Student(String name, String password) {
        this.name = name;
        this.password = password;
    }
}



package com.java.mapper;

import com.java.entity.Student;

/**
 * @author nidegui
 * @create 2019-06-25 14:49
 */
public interface StudentMapper {
    /**
     * 添加学生的信息
     * @param student
     */
    public int addStudnet(Student  student);
}

  

package com.java.util;

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;

/**
 * SqlSessionFactoty的工厂
 * @author nidegui
 * @create 2019-06-25 14:35
 */
public class SqlSessionFactotyUtil {
    private static SqlSessionFactory sqlSessionFactory;

    public static SqlSessionFactory getSqlSessionFactory(){
        if(sqlSessionFactory==null){
            InputStream inputStream=null;
            try{
                inputStream=Resources.getResourceAsStream("mybatis-config.xml");
                sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return sqlSessionFactory;
    }

    public static SqlSession openSession(){
        return getSqlSessionFactory().openSession();
    }

}

  

第四步: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="com.java.mapper.StudentMapper">

    <insert id="addStudnet" parameterType="Student" >
		insert into t_student values(null,#{name},#{password})
	</insert>

</mapper>

  

第五步:测试

package com.java.service;

import com.java.entity.Student;
import com.java.mapper.StudentMapper;
import com.java.util.SqlSessionFactotyUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * @author nidegui
 * @create 2019-06-25 14:57
 */
public class StudentService {

    public static void main(String[] args) {
        //获取数据库的链接
        SqlSession sqlSession = SqlSessionFactotyUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student=new Student("zhansan","12");
        int result = mapper.addStudnet(student);
        sqlSession.commit();//添加成功,要提交事务
        if(result>0){
            System.out.println("添加成功!");
        }

    }
}

 

 

posted on 2019-06-25 15:47  nidegui  阅读(582)  评论(0编辑  收藏  举报