zoukankan      html  css  js  c++  java
  • Mybatis快速入门Demo

    1:导入mybatis相关jar

    2:编写Mybatis配置文件(mybatis.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>
    
        <!-- 加载Src下的配置文件   -->
        <properties resource="db.properties"/>
        <!--别名机制-->
        <typeAliases>
            <typeAlias type="com.cn.entity.User" alias="user"/>
        </typeAliases>
    
        <!-- 连接mysql数据库   -->
        <environments default="mysql_development">
            <environment id="mysql_development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driverClass}"/>
                    <property name="url" value="${jdbcUrl}"/>
                    <property name="username" value="${user}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>
    
            <!--  连接Oracle数据库  -->
            <environment id="oracle_development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                    <property name="url" value="jdbc:oracle:thin:localhost:1521:orcl"/>
                    <property name="username" value="${user}"/>  <!--value的值 相当于mysql的数据库的名 在oracle中就是登陆的用户-->
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>

    </environments> <!-- 加载映射文件 --> <mappers> <mapper resource="com/cn/entity/mybatisUserMapper.xml"/> </mappers> </configuration>

    3编写实体bean 字段尽量与数据库中的表对应

    public class User {
    
        private int id;
        private String username;
        private Date birthday;
        private String sex;
        private String address;
    
        ....省get set....  
    }

    4实体与数据库的映射文件

    <?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.cn.entity.User" >
    
        <!--一个resultmap相当与一张数据表与实体的设 当实体与数据库表字段不一直时需要配置-->
    <!--    <resultMap id="accountResultMap" type="account" >-->
    <!--        <id column="acc_id" property="accId" jdbcType="INTEGER" />-->
    <!--        <result column="acc_login" property="accLogin" jdbcType="VARCHAR" />-->
    <!--        <result column="acc_name" property="accName" jdbcType="VARCHAR" />-->
    <!--        <result column="acc_pass" property="accPass" jdbcType="VARCHAR" />-->
    <!--    </resultMap>-->
    
        <!--用户登录-->
        <select id="findAll" resultType="user" >
            select  * from user;
      </select>
    
    </mapper>

    5测试的app

    public class TestApp {
        public static void main(String[] args) {
            try {
    
                /*
                    加载资源的一种方式:
                    InputStream in = Resources.getResourceAsStream("mybatis.xml");
                    //创建SqlSessionFactory
                    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
                    SqlSession sqlSession = sqlSessionFactory.openSession();
                    System.out.println(sqlSession);
                */
                SqlSession sqlSession = MyBatisUtil.getSqlSession();
                List<User> list = sqlSession.selectList("com.cn.entity.User.findAll");
                for(User u :list){
                    System.out.println(u);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    结果:

    User{id=1, username='zhangsan', birthday=Mon Feb 05 00:00:00 CST 2018, sex='男', address='湖南'}
    User{id=2, username='rose', birthday=Thu Mar 08 00:00:00 CST 2018, sex='女', address='东莞'}
    User{id=3, username='black', birthday=Fri Feb 19 00:00:00 CST 2021, sex='男', address='深圳'}
    User{id=4, username='pink_girl', birthday=Sat Aug 24 00:00:00 CST 2019, sex='女', address='changsan'}
    User{id=8, username='heheh', birthday=Sat Aug 24 00:00:00 CST 2019, sex='女', address='huana'}

     
    坚持
  • 相关阅读:
    LeetCode 64. 最小路径和
    LeetCode 344. 反转字符串
    LeetCode 162. 寻找峰值
    LeetCode 227. 基本计算器 II
    LeetCode 232. 用栈实现队列
    LeetCode 160. 相交链表
    LeetCode 112. 路径总和
    谈谈反作弊风控的实践经验
    LeetCode 704. 二分查找
    Hive SQL rank()/dense_rank()/row_number()的区别
  • 原文地址:https://www.cnblogs.com/gaoSJ/p/13038888.html
Copyright © 2011-2022 走看看