zoukankan      html  css  js  c++  java
  • mybatis01-1测试

    首先需要数据源信息和日志文件

    然后一个SQLMapConfig.xml配置文件连接数据库并且映射后一个xml文件,另一个xml文件写入SQL语句,

    最后text测试文件读取第一个配置文件,放到SQLSessionFactory工厂,SQLSession进行读取

    jdbc.properties

    log4j.properties

    User.java

    package pojo;
    
    public class User {
        private String phone;
        private String uname;
        private String upwd;
        private String email;
        private int role;
        public void setPhone(String phone) {
            this.phone = phone;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public void setUpwd(String upwd) {
            this.upwd = upwd;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public void setRole(int role) {
            this.role = role;
        }
        
        
        public String getPhone() {
            return phone;
        }
        public String getUname() {
            return uname;
        }
        public String getUpwd() {
            return upwd;
        }
        public String getEmail() {
            return email;
        }
        public int getRole() {
            return role;
        }
        @Override
        public String toString() {
            return "User [phone=" + phone + ", uname=" + uname + ", upwd=" + upwd + ", email=" + email + ", role=" + role
                    + "]";
        }
        
        
    
    }

    sqlMapConfig.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">
    <!-- mybatis 访问数据库的所有配置都在里边 -->
    <configuration>
       <!-- 配置数据源的信息 -->
       <environments default="mysql">
       
       
         <!-- id为程序员自定义的唯一标识 -->
         <!-- environment: 表示数据库厂商的配置环境 -->
         <environment id="mysql" >
           <!-- 1.配置事务模块 : 规定事务是由JDBC类型管理的-->
           <transactionManager type="JDBC"></transactionManager>
           <!-- 配置数据源信息 -->
              <!-- type="POOLED" 连接池 -->
              <dataSource type="POOLED">
                 <property name="driver" value="com.mysql.jdbc.Driver"/>
                 <property name="url" value="jdbc:mysql://localhost:3306/jsnu"/>
                 <property name="username" value="root"/>
                 <property name="password" value="12345678"/>
              </dataSource>
         </environment>
         
         
         <environment id="oracle" >
           <!-- 1.配置事务模块 : 规定事务是由JDBC类型管理的-->
           <transactionManager type="JDBC"></transactionManager>
           <!-- 配置数据源信息 -->
              <!-- type="POOLED" 连接池 -->
              <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="root"/>
                 <property name="password" value="12345678"/>
              </dataSource>
         </environment>
         
         
       </environments>
       
       
       <!-- 引入mapper的映射文件 -->
       <mappers>
           <!-- 每一个pojo对应一个XXXMapper.xml -->
           <!-- resource 包名/映射文件名 -->
           <mapper resource="pojo/UserMapper.xml"></mapper>
       </mappers>
    
    
    </configuration>

    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">
    <!-- namespace: 表示user对象的映射文件的命名空间
         1.其中的值, 是 改UserMapper.xml的唯一标识 -->
    <mapper namespace="pojo.UserMapper">
       <insert id="addUser">
           insert into t_user values (#{phone},#{uname},#{upwd},#{email},#{role})
       </insert>
       <update id="updateUser">
           update t_user set uname=#{uname} where phone=#{phone}
       </update>
       <delete id="deleteUser">
           delete from t_user where phone = #{phone}
       </delete>
       
       <!-- resultType ="包名,类名"
             1.作用: 根据mybatis根据类名, 自动封装成list -->
       <select id="queryUsers" resultType="pojo.User">
            select * from t_user where uname = #{uname}
       </select>
    
    </mapper>
  • 相关阅读:
    5-python基础—获取某个目录下的文件列表(适用于任何系统)
    Automated, Self-Service Provisioning of VMs Using HyperForm (Part 1) (使用HyperForm自动配置虚拟机(第1部分)
    CloudStack Support in Apache libcloud(Apache libcloud中对CloudStack支持)
    Deploying MicroProfile-Based Java Apps to Bluemix(将基于MicroProfile的Java应用程序部署到Bluemix)
    Adding Persistent Storage to Red Hat CDK Kit 3.0 (在Red Hat CDK Kit 3.0添加永久性存储)
    Carve Your Laptop Into VMs Using Vagrant(使用Vagran把您笔记本电脑刻录成虚拟机)
    使用Python生成一张用于登陆验证的字符图片
    Jupyter notebook的安装方法
    Ubuntu16.04使用Anaconda5搭建TensorFlow使用环境 图文详细教程
    不同时区的换算
  • 原文地址:https://www.cnblogs.com/xww115/p/10925313.html
Copyright © 2011-2022 走看看