MyBatis基本例子-实现增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis基本例子-实现增删改查相关的知识,希望对你有一定的参考价值。
1,项目结构
2,表单结构
3.代码
- 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的所有源代码!
只想说这一部分
1,book.javav中的属性名与mysql中book table中字段名字不匹配:
- <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>
2,命名空间
- <mapper namespace="com.mapper.BookMapper">
总体感觉没什么好说的!
1,读取配置文件获得SqlsessionFactory
2,通过SqlsessionFactory获得sqlsession
那涉及到一个问题:配置文件
配置文件:Configtion.xml
这个文件定义了关于数据源的配置
- <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>
封装数据库为一个POJO,也就是book.java
我们通过操作book对象操作数据库的记录!
那么类有了就会有类与数据库中表的关系的对应配置信息BookMapper.xml
其实这里与其说配置的是表和类的关系还不如说是对sql语句的关系的配置
参数是BOOK对象,与BOOK类建立了联系通过强大的#{}让字段名与类属性相关联,实现数据的填充!
不得不说,java的封装就是强!
类属性与表字段之间联系的确定,实现了对象和表中数据的关联!
个人见解,望广大网友指正!
以上是关于MyBatis基本例子-实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章