mybatis学习:一

来源:互联网 发布:js options的用法 编辑:程序博客网 时间:2024/06/05 01:51

导入需要使用到的jar包

1.创建表

CREATE TABLE `book` (  `bookId` int(11) NOT NULL auto_increment,  `bookName` varchar(20) default NULL,  `bookAuthor` varchar(200) default NULL,  `bookPrice` decimal(9,2) default NULL,  `bookInfo` longtext,  PRIMARY KEY  (`bookId`))

2.创建实体bean

public class Book implements Serializable{private int bookId;private String bookName;private String bookAuthor;private double bookPrice;private String bookInfo;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 double getBookPrice() {return bookPrice;}public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice;}public String getBookInfo() {return bookInfo;}public void setBookInfo(String bookInfo) {this.bookInfo = bookInfo;}@Overridepublic String toString() {return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", bookPrice="+ bookPrice + ", bookInfo=" + bookInfo + "]\r\n";}}

3.在src下创建配置文件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>    <typeAliases>        <typeAlias type="com.han.mybatis.vo.Book" alias="Book"/>    </typeAliases>    <!-- 配置环境(支持多个环境)  -->    <environments default="development">        <!-- 其中一个环境 -->        <environment id="development">            <!-- 事务管理(JDBC事务) -->            <transactionManager type="JDBC"></transactionManager>            <!-- 数据源 -->            <dataSource type="UNPOOLED">                <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="********"/>            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="com/han/mybatis/vo/BookMapper.xml"/>    </mappers></configuration>

4.创建方法映射文件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.han">    <select id="loadAll" resultType="Book">        select * from Book    </select>    <insert id="save" parameterType="Book">        insert into Book(bookName,bookAuthor,BookPrice,bookInfo)           values(#{bookName},#{bookAuthor},#{bookPrice},#{bookInfo})    </insert>    <delete id="delete" parameterType="int">        delete from Book where bookId=#{id}    </delete>    <update id="update" parameterType="Book">        update book set bookName=#{bookName},bookAuthor=#{bookAuthor},            bookPrice=#{bookPrice},bookInfo=#{bookInfo} where bookId=#{bookId}    </update>    <select id="loadById" resultType="Book" parameterType="int">        select * from book where bookId=#{id}    </select></mapper>

5.在主函数中测试

public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        InputStream inputStream = null;        SqlSession session = null;        try {            inputStream = Resources.getResourceAsStream("mybatis-config.xml");            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);            session=sessionFactory.openSession();            List<Book> books=session.selectList("com.han.loadAll");            System.out.println(books);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            session.close();            try {                inputStream.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}
原创粉丝点击