美文网首页
mybatis入门-查询所有

mybatis入门-查询所有

作者: 李霖神谷 | 来源:发表于2019-11-24 23:29 被阅读0次

    实现mybatis查询一张表中的所有数据
    1.新创建一个maven
    2.在pom文件中导入mybatis依赖,mysql依赖,junit测试依赖,在setting中设置本地仓库
    <dependencies>
    <dependency>
    <groupId>mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.26</version>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>
    </dependencies>
    3.在网上下载mybatis核心配置文件,映射配置文件
    核心配置文件:配置数据库,映射文件路径
    <?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="mysql">
    <environment id="mysql">
    <transactionManager type="JDBC"></transactionManager>
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/book"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="UserMapper.xml"></mapper>
    </mappers>
    </configuration>
    映射文件:namespace,id都是作为标识的作用
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="userMapper">
    <select id="findAll" resultType="com.shuai.dao.book">
    select * from p;
    </select>

    </mapper>
    4.实体类创建 book这里就不写了属性与数据库的字段名一一对应
    5..测试,selectList下面定义的就是mapper文件namespace,id标识的内容
    import com.shuai.dao.book;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;

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

    public class test {

    @Test
    public void findAllTest(){
        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("Config.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        List<book> bookList= sqlSession.selectList("userMapper.findAll");
        for (book book : bookList) {
            System.out.println(book);
        }
    }
    

    }
    值得一提的是mysql1.8版本以上,mysql导入依赖的时候版本要一致,配置文件驱动要改一下,url后面也要添加一个参数:

    <?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"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mypeople?serverTimezone=UTC"/>
                    <property name="username" value="root"/>
                    <property name="password" value="3041"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="peopleM.xml"/>
        </mappers>
    </configuration>
    

    相关文章

      网友评论

          本文标题:mybatis入门-查询所有

          本文链接:https://www.haomeiwen.com/subject/aaerwctx.html