• Mybatis简单的入门之增删改查


    一般的过程例如以下
    1、加入Mybatis所须要的包,和连接数据库所需的包
    2、配置mybatis-config.xml文件

    3、配置与pojo相应的映射文件

    mybatis-config,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>
    <typeAliases><!--类型别名-->
    <typeAlias alias="User" type="com.iss.pojo.User" />
    </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://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    </dataSource>
    </environment>
    </environments>


    <mappers>
    <mapper resource="com/iss/pojo/User.xml" /><!--映射文件的路径-->
    </mappers>
    </configuration>


    pojo实体类

    package com.iss.pojo;


    public class User {
    private int id;


    private String name;


    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 User(int id, String name) {
    super();
    this.id = id;
    this.name = name;
    }


    public User() {
    super();
    }


    }



    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="User"><!--映射的命名空间也就是在mybatis中配置的别名-->
    <select id="selectUser" parameterType="int" resultType="User"><!--在mybatis-config.xml中为类型起了别名-->
    <![CDATA[
            select * from user where id = #{id}]]><!--查询的是表名不是对象-->
    </select>
    <insert id="insertUser" parameterType="User">

    <![CDATA[insert into user
    value(#{id},#{name})]]>


    </insert>
    <delete id="deleteUser" parameterType="int">
    delete from user where
    id=#{id}


    </delete>
    <update id="updateUser" parameterType="User">
    update user set
    name=#{name} where id=#{id}
    </update>
    </mapper>

    Mybatis工具类

    Mybatis工具类

    package com.iss.util;


    import java.io.IOException;
    import java.io.Reader;


    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 com.sun.org.apache.xpath.internal.operations.Gte;


    public class MybatisUtil {


    private static SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//类似于Hibernate中Configration


    private static SqlSessionFactory sqlSessionFactory;//类似于Hibernate中SessionFactory


    // 静态初始化块配置文件在类执行时仅仅载入一次
    static {


    String resource = "mybatis-config.xml";
    Reader reader = null;
    try {
    reader = Resources.getResourceAsReader(resource);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    sqlSessionFactory = builder.build(reader);
    }


    private MybatisUtil() {
    // TODO Auto-generated constructor stub
    }


    public static SqlSessionFactoryBuilder getBuilder() {
    return builder;
    }


    public static void setBuilder(SqlSessionFactoryBuilder builder) {
    MybatisUtil.builder = builder;
    }


          
    public static SqlSessionFactory getSqlSessionFactory() {
    return sqlSessionFactory;
    }


    public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    MybatisUtil.sqlSessionFactory = sqlSessionFactory;
    }


    // 获得sqlSession对象类似于Hibernate中的session
    public static SqlSession getSqlSession() {
    SqlSession sqlSession = sqlSessionFactory != null ? sqlSessionFactory
    .openSession() : null;
    return sqlSession;
    }


    public static void closeSession() {
    SqlSession sqlSession = getSqlSession();
    if (sqlSession != null) {
    sqlSession.close();
    }
    }


    }

    UserService

    package com.iss.service;


    import org.apache.ibatis.session.SqlSession;


    import com.iss.pojo.User;
    import com.iss.util.MybatisUtil;


    public class UserService {


    public static User selectUser(int id) {
    SqlSession sqlSession = MybatisUtil.getSqlSession();
    User user = sqlSession.selectOne("selectUser", 1);
    MybatisUtil.closeSession();
    return user;
    }


    public static void insertUser(int id, String name) {


    User user = new User();
    user.setId(id);
    user.setName(name);


    SqlSession sqlSession = MybatisUtil.getSqlSession();
    sqlSession.insert("insertUser", user);
    sqlSession.commit();
    MybatisUtil.closeSession();
    }


    public static void updateUser(int id, String name) {
    User user = new User();
    user.setId(id);
    user.setName(name);


    SqlSession sqlSession = MybatisUtil.getSqlSession();
    sqlSession.update("updateUser", user);
    sqlSession.commit();
    MybatisUtil.closeSession();


    }


    public static void deleteUser(int id) {
    SqlSession sqlSession = MybatisUtil.getSqlSession();
    sqlSession.delete("deleteUser", id);
    sqlSession.commit();
    MybatisUtil.closeSession();
    }


    }

    TestUser

    package com.iss.test;


    import com.iss.pojo.User;
    import com.iss.service.UserService;


    public class TestUser {


    public static void main(String[] args) {


    UserService.insertUser(3, "world");
    User user = UserService.selectUser(2);
    System.out.println(user.getName());
    UserService.updateUser(1, "wdy");
    System.out.println(UserService.selectUser(2).getName());


    UserService.deleteUser(2);
    }
    }


    总结Hibernate 和Mybatis非常相似

    基本的差别

    Hibernate的映射文件是对表的映射

    Mybatis的映射文件是对表操作的一种映射




  • 相关阅读:
    追加上传
    反面教材 构造构造 json 数据
    Reading table information for completion of table and column names
    (原创)c#学习笔记04--流程控制03--分支03--switch语句
    (原创)c#学习笔记04--流程控制03--分支02--if语句
    (原创)c#学习笔记04--流程控制03--分支01--三元运算符
    (原创)c#学习笔记04--流程控制02--goto语句
    (原创)c#学习笔记04--流程控制01--布尔逻辑02--按位运算符
    (原创)c#学习笔记04--流程控制01--布尔逻辑01--布尔赋值运算符
    (原创)c#学习笔记03--变量和表达式04--表达式04--命名空间
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4004865.html
Copyright © 2020-2023  润新知