• Mybatis学习(一)


          MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。

          MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。

          MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录.

    快速入门示例:

    1、引入需要的jar包

    • mybatis
    • MYSQL驱动包

    2、数据库建表,此处以Users表为例

    create database mybatis;
    
    use mybatis;
    
    CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT);
    
    INSERT INTO users(NAME, age) VALUES('张三', 20);
    
    INSERT INTO users(NAME, age) VALUES('李四', 25);

    3、添加mybatis 配置文件 conf.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>
        <environments default="development">
           <environment id="development">
             <transactionManager type="JDBC">
             </transactionManager>
             <dataSource type="POOLED">
                    <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="123456" />
             </dataSource>
           </environment>
        </environments>
    </configuration>

     4、定义表对应的实体类

    package com.mybatis.bean;
    
    public class User {
    
        private int id;
        private String name;
        private int age;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }

    5、定义操作users表的sql映射文件userMapper.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.mybatis.bean.User">
    <select id="getUser" parameterType="int" resultType="com.mybatis.bean.User">
       select * from user where id=#{id}
    </select>
    </mapper>

      6、在conf.xml文件中注册userMapper.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>
        <environments default="development">
           <environment id="development">
             <transactionManager type="JDBC">
             </transactionManager>
             <dataSource type="POOLED">
                    <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="123456" />
             </dataSource>
           </environment>
        </environments>
        <mappers>
            <mapper resource="userMapper.xml"></mapper>
        </mappers>
    </configuration>

     7、编写测试代码

    package com.mybatis.service;
    
    import java.io.IOException;
    import java.io.Reader;
    
    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 com.mybatis.bean.User;
    
    public class test {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
    
            String pathxml = "conf.xml";
            
            Reader reader = Resources.getResourceAsReader(pathxml);
            SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession session = sessionfactory.openSession();
            
            String statement = "com.mybatis.bean.User.getUser";
            User user = session.selectOne(statement,1); // 1 为要查询的条件id
            
            System.out.println(user.getName());
        }
    
    }
  • 相关阅读:
    pytorch bug: for step,data in enumerate(loader)+Connection reset by peer
    pytorch bug
    ImportError: No module named '_tkinter', please install the python3-tk package
    nvidia-docker+cuda8.0+ubuntu16.04
    召回率,精确率,mAP如何计算
    tensorflow 迭代周期长,每个epoch时间变慢
    yolov3中 预测的bbox如何从特征图映射到原图?
    知乎问题:目标检测领域还有什么可以做的?
    目标检测数据增强,旋转方法
    OpenBLAS(1)----安装OpenBLAS
  • 原文地址:https://www.cnblogs.com/JsonShare/p/5211583.html
Copyright © 2020-2023  润新知