mybits最基本的例子---------实现增删改查

mybits最基本的例子---------实现增删改查

1,项目结构




2,表单结构


3,项目代码

3.1 book.java

package com.entity;

public class Book {

	private int bookid;
	private String bookname;
	private String bookauthor;
	private int bookprice;

	public Book() {
		super();
	}

	public Book(int bookid, String bookname, String bookauthor, int bookprice) {
		super();
		this.bookid = bookid;
		this.bookname = bookname;
		this.bookauthor = bookauthor;
		this.bookprice = bookprice;
	}
	
	public Book(String bookname, String bookauthor, int bookprice) {
		super();
		this.bookname = bookname;
		this.bookauthor = bookauthor;
		this.bookprice = bookprice;
	}

	public int getBookid() {
		return bookid;
	}

	public void setBookid(int bookid) {
		this.bookid = bookid;
	}

	public String getBookname() {
		return bookname;
	}

	public void setBookname(String bookname) {
		this.bookname = bookname;
	}

	public String getBookauthor() {
		return bookauthor;
	}

	public void setBookauthor(String bookauthor) {
		this.bookauthor = bookauthor;
	}

	public int getBookprice() {
		return bookprice;
	}

	public void setBookprice(int bookprice) {
		this.bookprice = bookprice;
	}

}
BookMapper.xml




	
		
		
		
		
	

	

	
		delete * from book where id =
		#{id}
	

	
		insert into book
		values(null,#{bookname},#{bookauthor},#{bookprice})
	

	


Configuration.xml



	
		
			
			
				
				?characterEncoding=utf-8">
				
				
			
		
	
	
	
		
	

3.4 MysqlSessionFactory

package com.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;


public class MysqlSessionFactory {
	static SqlSession session;
	public SqlSession getSqlSession(){
		try {
			Reader reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactory  sessionFactory = new SqlSessionFactoryBuilder().build(reader);
			session = sessionFactory.openSession();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return session;
	}

}
Test.java
package com.util;

import org.apache.ibatis.session.SqlSession;

import com.entity.*;

public class Test {
	
	public static void main(String[] args) {
		
		MysqlSessionFactory mysqlSessionFactory = new MysqlSessionFactory();
		SqlSession session = mysqlSessionFactory.getSqlSession();
		
		System.out.println(session);
		
		Book book = (Book)session.selectOne("com.mapper.BookMapper.findById",25);
		System.out.println(book);
		session.commit();
		
		
		Book bookadd = new Book(0, "java","mike",90);
		session.insert("com.mapper.BookMapper.add",bookadd);
		session.commit();
		
		
		
		
	}

}

以上是整个demo的所有源代码!


只想说这一部分

1,book.javav中的属性名与mysql中book table中字段名字不匹配:

	
	
	
	
resulteMap   是为了解决book.javav中的属性名与mysql中book table中字段名字不匹配,无法进行数据的填充!

2,命名空间
这个是service的名字,不要写错,另外service的方法名也要和配置文件的方法名一样。

总体感觉没什么好说的!
1,读取配置文件获得SqlsessionFactory
2,通过SqlsessionFactory获得sqlsession
那涉及到一个问题:配置文件
配置文件:Configtion.xml
这个文件定义了关于数据源的配置

		
			
			
				
				value="jdbc:mysql://localhost:3306/
				
				
			
		
	
那么这里有了连接mysql数据库的必要信息但是我们的目的是通过java语句操作数据库
封装数据库为一个POJO,也就是book.java
我们通过操作book对象操作数据库的记录!

那么类有了就会有类与数据库中表的关系的对应配置信息BookMapper.xml
其实这里与其说配置的是表和类的关系还不如说是对sql语句的关系的配置
参数是BOOK对象,与BOOK类建立了联系通过强大的#{}让字段名与类属性相关联,实现数据的填充!

不得不说,java的封装就是强!
类属性与表字段之间联系的确定,实现了对象和表中数据的关联!

个人见解,望广大网友批评指正!


你可能感兴趣的:(Java,java,MYBITS,框架)