其他分享
首页 > 其他分享> > mybatis01-1测试

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>

 

标签:String,phone,upwd,uname,role,测试,public,mybatis01
来源: https://www.cnblogs.com/xww115/p/10925313.html