mybatis 图书 插入 更新 删除 查询

CustomerMapper.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">

<!-- namespace表示命名空间 -->

<mapper namespace="com.itheima.mapper.CustomerMapper">

    <!--根据客户编号获取客户信息 -->

    <select id="findbookById" parameterType="String"

       resultType="com.itheima.po.Book">

       select * from book where id = #{id}

    </select>

    <!--根据客户名模糊查询客户信息列表-->

    <select id="findbookByName" parameterType="String"

        resultType="com.itheima.po.Book">

        <!-- select * from book where name like '%${value}%' -->

        select * from book where name like '%${value}%'

    </select>

    <!-- 添加客户信息 -->

    <insert id="addName" parameterType="com.itheima.po.Book">

        insert into book(name,author,price,press)

        values(#{name},#{author},#{price},#{press})

    </insert>

    <!-- 更新客户信息 -->

    <update id="updatebook" parameterType="com.itheima.po.Book">

        update book set

       id=#{id},name=#{name},author=#{author},price=#{price},press=#{press}

       where id=#{id}

    </update>

    <!-- 删除客户信息 -->

    <delete id="deletebook" parameterType="String">

        delete from book where id=#{id}

    </delete>

</mapper>

Book.java

package com.itheima.po;

public class Book {

    private String id;

    private String name;

    private String author;

    private Double price;

    private String press;

    public String getId() {

       return id;

    }

    public void setId(String id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getAuthor() {

       return author;

    }

    public void setAuthor(String author) {

       this.author = author;

    }

    public Double getPrice() {

       return price;

    }

    public void setPrice(Double price) {

       this.price = price;

    }

    public String getPress() {

       return press;

    }

    public void setPress(String press) {

       this.press = press;

    }

    public String toString() {

       return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", press=" + press

              + "]";

    }

}

MybatisTest.java

package com.itheima.test;

import java.io.InputStream;

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 org.junit.Test;

import com.itheima.po.Book;

import java.util.List;

public class MybatisTest {

/*依书号查询*/ 

    @Test

    public void findbookByIdTest() throws Exception {

       String resource = "mybatis-config.xml";

       InputStream inputStream =

              Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory =

              new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       Book book = sqlSession.selectOne("com.itheima.mapper"+

                  ".CustomerMapper.findbookById","42706");

       System.out.println(book.toString());

       sqlSession.close();

       System.out.println();

    }

/*模糊查询*/   

    @Test

    public void findbookByNameTest() throws Exception {

       String resource = "mybatis-config.xml";

       InputStream inputStream = Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory =

              new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       List<Book> books = sqlSession.selectList("com.itheima.mapper"

              + ".CustomerMapper.findbookByName","s");

       for (Book book : books) {

       System.out.println(book);

       }

       sqlSession.close();

       System.out.println();

    }

/*插入*/  

    @Test

    public void addNameTest() throws Exception {

       String resource = "mybatis-config.xml";

       InputStream inputStream = Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory =

              new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       Book book = new Book();

       book.setId("042715");

       book.setName("aichi");

       book.setAuthor("jueshi");

       book.setPrice(88.70);

       book.setPress("yitongbanshe");

       int rows = sqlSession.insert("com.itheima.mapper"

              + ".CustomerMapper.addName",book);

       if(rows > 0){

           System.out.println("成功插入了" + rows + "条数据!");

       }

       else{

           System.out.println("执行插入操作失败!");

       }

       sqlSession.commit() ;

       System.out.println();

    }

/*更新(修改)*/   

    @Test

    public void updatebookTest() throws Exception {

       String resource = "mybatis-config.xml";

       InputStream inputStream = Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory =

              new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       Book book = new Book();

       book.setId("42712");

       book.setName("beijin");

       book.setAuthor("gugong");

       book.setPrice(99.80);

       book.setPress("tiantangbanshe");

       int rows = sqlSession.update("com.itheima.mapper"

              + ".CustomerMapper.updatebook",book);

       if(rows > 0){

           System.out.println("成功修改了" + rows + "条数据!");

       }

       else{

           System.out.println("执行修改操作失败!");

       }

       sqlSession.commit();

       sqlSession.close();

       System.out.println();

    }

/*删除*/

    @Test

    public void deletebookTest() throws Exception {

       String resource = "mybatis-config.xml";

       InputStream inputStream =

              Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory =

              new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       int rows = sqlSession.delete("com.itheima.mapper"+

                  ".CustomerMapper.deletebook","42721");

       if(rows > 0){

           System.out.println("您成功删除了" + rows + "条数据!");

       }

       else{

           System.out.println("执行删除操作失败!");

       }

       sqlSession.close();

       System.out.println();

    }

}  

log4j.properties

# Global logging configuration

log4j.rootLogger=ERROR, stdout

# MyBatis logging configuration...

log4j.logger.com.itheima=DEBUG

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

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>

    <!--1.配置环境 ,默认的环境id为mysql-->

    <environments default="mysql">

        <!--1.2.配置id为mysql的数据库环境 -->

        <environment id="mysql">

            <!-- 使用JDBC的事务管理 -->

            <transactionManager type="JDBC" />

            <!--数据库连接池 -->

            <dataSource type="POOLED">

             <property name="driver" value="com.mysql.jdbc.Driver" />

             <property name="url"

                            value="jdbc:mysql://localhost:3306/bookdata" />

             <property name="username" value="root" />

             <property name="password" value="root" />

            </dataSource>

        </environment>

    </environments>

    <!--2.配置Mapper的位置 -->

    <mappers>

       <mapper resource="com/itheima/mapper/CustomerMapper.xml" />

    </mappers>

</configuration>

截图:

猜你喜欢

转载自www.cnblogs.com/1314-520/p/12793020.html