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
<?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.mapper.BookMapper">
<span style="white-space:pre"> </span><resultMap id="mapBook" type="com.entity.Book">
<id property="bookid" column="id"></id>
<result property="bookname" column="username"></result>
<result property="bookprice" column="price"></result>
<result property="bookauthor" column="author"></result>
</resultMap></span></strong>
<select id="findById" parameterType="int" resultMap="mapBook">
select *
from book where id = #{id}
</select>
<delete id="del" parameterType="int">
delete * from book where id =
#{id}
</delete>
<insert id="add" parameterType="com.entity.Book">
insert into book
values(null,#{bookname},#{bookauthor},#{bookprice})
</insert>
<select id="show" resultMap="mapBook">
select * from book
</select>
</mapper>
Configuration.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 default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/<span style="color:#ff6666;"><mysqlDb></span>?characterEncoding=utf-8"></property>
<property name="username" value=mysqlnam"></property>
<property name="password" value="mysqlpassword"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/BookMapper.xml"></mapper>
</mappers>
</configuration>
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的所有源代碼!
<resultMap id="mapBook" type="com.entity.Book">
<id property="bookid" column="id"></id>
<result property="bookname" column="username"></result>
<result property="bookprice" column="price"></result>
<result property="bookauthor" column="author"></result>
</resultMap>
resulteMap 是爲了解決book.javav中的屬性名與mysql中book table中字段名字不匹配,無法進行數據的填充!<mapper namespace="com.mapper.BookMapper">
這個是service的名字,不要寫錯,另外service的方法名也要和配置文件的方法名一樣。<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" <span style="font-family: Arial, Helvetica, sans-serif;">value="jdbc:mysql://localhost:3306/</span><span style="font-family: Arial, Helvetica, sans-serif;"></property></span>
<property name="username" value="mysqlnam"></property>
<property name="password" value="mysqlpassword"></property>
</dataSource>
</environment>
</environments>
那麼這裏有了連接mysql數據庫的必要信息但是我們的目的是通過java語句操作數據庫