zoukankan      html  css  js  c++  java
  • 06mybatis-CRUD操作

    mybatis CRUD 操作
    ----------------------------------
    db.properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/db?useSSL=true&useUnicode=true&characterEncoding=UTF8
    jdbc.username=root
    jdbc.password=root


    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>
    <properties resource="db.properties"/>
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <!-- 配置接口 -->
    <!--<mapper class="com.fz.mapper.BookMapper"/>-->

    <!-- 映射配置文件 -->
    <!--<mapper resource="com/fz/mapper/BookMapper.xml"/>-->

    <!-- 指定mapper接口所在的包 -->
    <package name="com.fz.mapper"/>
    </mappers>
    </configuration>


    com.fz.mapper.BookMapper.java 接口
    package com.fz.mapper;

    import com.fz.entity.Book;
    import org.apache.ibatis.annotations.Select;

    import java.util.List;

    /**
    * Created by webrx on 2017-06-13.
    */
    public interface BookMapper {
    @Select("select * from book")
    public List<Book> query();
    }

    mybatis-config.xml 文件配置
    <mappers>
    <mapper class="com.fz.mapper.BookMapper"/>
    </mappers>

    程序代码
    BookMapper bdao = ss.getMapper(BookMapper.class);
    List<Book> bks = bdao.query();
    for(Book book : bks){
    System.out.println(book.getName());
    }

    com.fz.mapper.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.fz.mapper.BookMapper">
    <select id="query" resultType="com.fz.entity.Book">
    select * from book
    </select>
    </mapper>

    mybatis-config.xml 文件配置
    <mappers>
    <mapper class="com.fz.mapper.BookMapper"/>
    </mappers>

    程序代码
    inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    new SqlSessionFactoryBuilder().build(inputStream);
    ss = sf.openSession();
    List<Book> bks = ss.selectList("com.fz.mapper.BookMapper.query");
    for(Book book : bks){
    System.out.println(book.getName());
    }

    测试代码
    import com.fz.entity.Book;
    import com.fz.mapper.BookMapper;
    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.Before;
    import org.junit.Test;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;

    /**
    * Created by webrx on 2017-06-13.
    */
    public class Demo {
    protected SqlSessionFactory sf;
    protected SqlSession ss;

    @Before
    public void init(){
    InputStream inputStream = null;
    try {
    inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    } catch (IOException e) {
    e.printStackTrace();
    }
    sf = new SqlSessionFactoryBuilder().build(inputStream);
    ss = sf.openSession();
    }

    @Test
    public void show(){
    BookMapper bdao = ss.getMapper(BookMapper.class);

    //插入
    //Book b = new Book();
    //b.setName("《javascript案例设计》");
    //b.setPrice(80d);
    //bdao.save(b);


    //删除
    bdao.deleteById(4);

    ss.commit();

    //List<Book> bks = bdao.query();

    List<Book> bks = ss.selectList("com.fz.mapper.BookMapper.query");
    for(Book book : bks){
    System.out.println(book.getName());
    }
    }
    }

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    tomcat 启动 报错Neither the JAVA_HOME nor the JRE_HOME environment variable is definedtemp
    tomcat linux 加入服务自动启动
    registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    tomcat 8 加 struts2的 java.lang.NoSuchFieldException: resourceEntries
    tomcat Can't create cache file!
    tomcat 部署时修改服务器时间
    tomcat java变量环境设置
    scrapy 动态IP、随机UA、验证码
    scrapy xpath、正则表达式、css选择器
    Saltstack windows可视化操作(十四)
  • 原文地址:https://www.cnblogs.com/Mkady/p/7001711.html
Copyright © 2011-2022 走看看