1. 程式人生 > 實用技巧 >MyBayis註解之CRUD

MyBayis註解之CRUD

MyBayis註解之CRUD

準備配置檔案

點選檢視完整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>
   
    <!-- environments配置要連線的資料庫 -->
    <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/nyf?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    
    <!-- mappers,指明實體類對應的配置檔案 -->
    <mappers>
        <mapper class="com.nyf.mappers.CategoryMapper"/> 
    </mappers>
</configuration>

準備Mapper

點選檢視完整CategoryMapper

package com.nyf.mappers;

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.nyf.pojo.Category;

public interface CategoryMapper {
	@Insert(" insert into category_ ( name ) values (#{name}) ") 
    public int add(Category category); 
        
    @Delete(" delete from category_ where id= #{id} ") 
    public void delete(int id); 
        
    @Select("select * from category_ where id= #{id} ") 
    public Category get(int id); 
      
    @Update("update category_ set name=#{name} where id=#{id} ") 
    public int update(Category category);  
        
    @Select(" select * from category_ ") 
    public List list(); 
}

準備測試類

點選檢視完整CRUD

package com.nyf.tests;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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.nyf.pojo.Category;
import com.nyf.mappers.CategoryMapper;

public class CRUD {
	public static void main(String[] args) throws IOException {
		String resource = "com/nyf/config/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		CategoryMapper mapper = session.getMapper(CategoryMapper.class);

		add(mapper);
		delete(mapper);
		get(mapper);
		update(mapper);

		session.commit();
		session.close();

	}

	private static void add(CategoryMapper mapper) {
		System.out.println("執行add方法:");
		Category c = new Category();
		c.setName("新增加的Category");
		mapper.add(c);
		listAll(mapper);
	}
	
	private static void delete(CategoryMapper mapper) {
		System.out.println("執行delete方法:");
		mapper.delete(2);
		listAll(mapper);
	}
	
	private static void update(CategoryMapper mapper) {
		Category c = mapper.get(1);
		c.setName("修改了的Category名稱");
		mapper.update(c);
		listAll(mapper);
	}

	private static void get(CategoryMapper mapper) {
		System.out.println("執行get方法:");
		Category c = mapper.get(1);
		System.out.println(c.getName());
	}
	
	private static void listAll(CategoryMapper mapper) {
		System.out.println("執行listAll方法:");
		List cs = mapper.list();
		for (Category c : cs) {
			System.out.println(c.getName());
		}
	}
}

結果驗證

正確結果應該如下所示:

執行add方法:
執行listAll方法:
category
category
新增加的Category
執行delete方法:
執行listAll方法:
category
新增加的Category
執行get方法:
category
執行listAll方法:
修改了的Category名稱
新增加的Category