Mybatis框架基于注解的方式,实对数据现增删改查

编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可;

在lib下 导入mybatis架包:mybatis-3.1.1.jar
mysql驱动架包:mysql-connector-java-5.1.6-bin.jar

 

在src目录下建立xml配置文件:conf.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:开发模式
    work:工作模式
    default="development",id="development",两个的属性值必须一致
     -->    
     <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/mybatis"/>
                 <property name="username" value="root"/>
                 <property name="password" value="lxn123"/>
             </dataSource>    
         </environment>
     </environments>
     
     <!-- 在配置文件中 关联包下的 接口类-->
     <mappers>
         <mapper class="com.atguigu.mybatis.test3.UserMapper"/>
     </mappers>
</configuration>

 

这是基于注解的方式,所以建立一个接口,在定义的方法上配置上应有的注解,注解中含有sql语句,需要提示的是这个接口不需要类去实现它

package com.atguigu.mybatis.test3;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.atguigu.mybatis.test.User;

public interface UserMapper {
    
    /*
     * 这是基于注解的映射方式,实现对数据的增删改查,将sql语句直接写在注解的括号中
     * 这是一个接口,其不需要类去实现它
     * 下边分别是插入,删除,修改,查询一个记录,查询所有的记录
     * */
    
    @Insert("insert into users(name,age) values(#{name},#{age})")
    public void insertT(User user);
    
    @Delete("delete from users where id=#{id}")
    public void deleteById(int id);
    
    @Update("update users set name=#{name},age=#{age} where id=#{id}")
    public void updateT(User user);
    
    @Select("select * from users where id=#{id}")
    public User getUser(int id);
    
    @Select("select * from users")
    public List<User> getAllUsers();
}

 

封装列:User,这儿只写属性,getter和setter,tostring就不写了

private int id;
private String name;
private int age;

 

建立MybatisUtils类,有一个方法,加载mybatis和构建sqlSession

package com.atguigu.mybatis.test;

import java.io.InputStream;

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

public class MybatisUtils {
    public static SqlSessionFactory getFactory(){
        String resource="conf.xml";
        
        //加载mybatis 的配置文件(它也加载关联的映射文件)
        InputStream is=MybatisUtils.class.getClassLoader().getResourceAsStream(resource);
        
        //构建sqlSession 的工厂
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
        return factory;
    }
}

 

实现增删改查的方法

package com.atguigu.mybatis.test3;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import com.atguigu.mybatis.test.MybatisUtils;
import com.atguigu.mybatis.test.User;

public class UserMapperTest {
    
    @Test
    //插入数据
    public void testInsert(){
        SqlSessionFactory factory=MybatisUtils.getFactory();
        SqlSession session=factory.openSession(true);
        //使用反射的方法
        UserMapper mapper=session.getMapper(UserMapper.class);
        mapper.insertT(new User(-1, "p", 4));
        
        session.close();
    }
    
    @Test
    //删除数据
    public void testDelete(){
        SqlSessionFactory factory=MybatisUtils.getFactory();
        SqlSession session=factory.openSession(true);
        UserMapper mapper=session.getMapper(UserMapper.class);
        mapper.deleteById(1);
        session.close();
    }
    
    @Test
    //修改数据
    public void testUpdate(){
        SqlSessionFactory factory=MybatisUtils.getFactory();
        SqlSession session=factory.openSession(true);
        UserMapper mapper=session.getMapper(UserMapper.class);
        mapper.updateT(new User(2, "jjjjj", 232));
        session.close();
    }
    
    @Test
    //获取一条数据
    public void testGetUser(){
        SqlSessionFactory factory=MybatisUtils.getFactory();
        SqlSession session=factory.openSession(true);
        UserMapper mapper=session.getMapper(UserMapper.class);
        User user=mapper.getUser(2);
        session.close();
        System.out.println(user);
    }
    
    @Test
    //获取所有数据
    public void testGetAllUsers(){
        SqlSessionFactory factory=MybatisUtils.getFactory();
        SqlSession session=factory.openSession(true);
        UserMapper mapper=session.getMapper(UserMapper.class);
        List<User> users=mapper.getAllUsers();
        
        session.close();
        System.out.println(users);
    }
}

 

    原文作者:MyBatis
    原文地址: https://www.cnblogs.com/lxnlxn/p/5996707.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞