• MyBatis3入门样例


    User.java

    package com.archie.domain;

    import java.io.Serializable;

    @SuppressWarnings(
    "serial")
    public class User implements Serializable{

    private int uid;
    private String uname;
    private String upwd;

    public int getUid() {
    return uid;
    }
    public void setUid(int uid) {
    this.uid = uid;
    }
    public String getUname() {
    return uname;
    }
    public void setUname(String uname) {
    this.uname = uname;
    }
    public String getUpwd() {
    return upwd;
    }
    public void setUpwd(String upwd) {
    this.upwd = upwd;
    }
    @Override
    public String toString() {
    return "User [uid=" + uid + ", uname=" + uname + ", upwd=" + upwd + "]";
    }

    }

    UserMapper.java

     
    package com.archie.persistence;

    import com.archie.domain.User;

    public interface UserMapper {

    User getUserByName(String uname);
    }

    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.archie.persistence.UserMapper">

    <cache />

    <select id="getUserByName" resultType="User" parameterType="string">
    select * from t_user
    where uname=#{uname}
    </select>

    </mapper>

    mybatis-config.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>
    <settings>
    <!-- changes from the defaults -->
    <setting name="lazyLoadingEnabled" value="false" />
    </settings>

    <typeAliases>

    <typeAlias alias="User" type="com.archie.domain.User" />

    </typeAliases>

    <environments default="development">

    <environment id="development">

    <transactionManager type="JDBC" />

    <dataSource type="POOLED">

    <property name="driver" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/ibatisorm" />

    <property name="username" value="root" />

    <property name="password" value="root" />

    </dataSource>

    </environment>

    </environments>

    <mappers>

    <mapper resource="com/archie/persistence/UserMapper.xml" />

    </mappers>

    </configuration>

    MyBatisTest.java  

     package com.archie.test;

    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.archie.domain.User;

    /**
    * MyBatis内部测试
    *
    @author archie2010
    * since 2011-3-2 下午02:34:17
    */
    public class MyBatisTest {

    public static void main(String[] args) throws IOException {

    String resource
    ="mybatis-config.xml";

    Reader reader
    = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory
    =new SqlSessionFactoryBuilder().build(reader);

    SqlSession session
    =sessionFactory.openSession();

    User user
    =(User) session.selectOne("com.archie.persistence.UserMapper.getUserByName", "archie");

    System.out.println(user.toString());

    session.close();
    }
    }
  • 相关阅读:
    php文件下载原理
    spring源码@configuration&@bean
    解决java在idea运行正常,但是打成jar包后中文乱码问题
    IntelliJ IDEA查看堆内存和类继承关系
    jenkins无法展示报告
    解决windows(slave)导致linux(master)输出乱码
    UIRECODER安装记录
    vue项目前后端部署
    django orm
    django笔记
  • 原文地址:https://www.cnblogs.com/archie2010/p/1969181.html
Copyright © 2020-2023  润新知