• Mybatis初学


    Mybatis简介大家可以自行google之。官网为:http://code.google.com/p/mybatis/

    我也刚刚开始初学,先来一个小例子:

    创建一个User.java文件,代码如下:

    package com.rollen;
    
    public class User {
    	private String name;
    	private int age;
    	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;
    	}
    	@Override
    	public String toString() {
    		
    		return "Name:"+name+"Age"+age;
    	}
    	
    	
    }
    

      上面的java文件位于com.rollen包中,然后在这个包中建立generator.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>  
        <typeAliases>  
            <typeAlias alias="User" type="com.rollen.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/user_db" />  
                    <property name="username" value="root" />  
                    <property name="password" value="root" />  
                </dataSource>  
            </environment>  
        </environments>  
        <mappers>  
            <mapper resource="com/rollen/user.xml" />  
        </mappers>  
    </configuration>  
    

      在com.rollen包下面创建user.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.rollen.User">
    	<select id="selectUser" parameterType="int" resultType="User" >
    		 select * from user where age = #{age} 
    	</select>
    </mapper>
    

    我们最后编写一个测试类,代码如下“:

    package com.rollen;
    import java.io.*;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class Test {
    	public static void main(String[] args) {
    		String resource="com/rollen/generatorConfig.xml";
    		Reader reader = null;
    		try {
    			reader = Resources.getResourceAsReader(resource);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
    		SqlSession sqlSession = factory.openSession();
    		
    		try {
    			User user=sqlSession.selectOne("selectUser", 10);
    			System.out.println(user);
    		}finally{
    			sqlSession.close();
    		}
    		
    	}
    }
    

      在上面的generateConfig.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>
    	<properties>
    		<property name="username" value="root" />
    	</properties>
    	<typeAliases>
    		<typeAlias alias="User" type="com.rollen.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/user_db" />
    				<property name="username" value="${username}" />
    				<property name="password" value="root" />
    			</dataSource>
    		</environment>
    	</environments>
    	<mappers>
    		<mapper resource="com/rollen/user.xml" />
    	</mappers>
    </configuration>  
    

      注意,和上面的区别是我们添加了:

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

      并且修改了这行代码:

    <property name="username" value="${username}" />
    

      还可以有如下的修改方法,新建一个config.properties文件,文件内容为:

    password=root
    

      然后将generatorconfig.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>
    	<properties resource="com/rollen/config.properties">
    		<property name="username" value="root" />
    	</properties>
    	<typeAliases>
    		<typeAlias alias="User" type="com.rollen.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/user_db" />
    				<property name="username" value="${username}" />
    				<property name="password" value="${password}" />
    			</dataSource>
    		</environment>
    	</environments>
    	<mappers>
    		<mapper resource="com/rollen/user.xml" />
    	</mappers>
    </configuration>  
    

      需要注意的是如下的两行代码:

    <properties resource="com/rollen/config.properties">
    ....
    <property name="password" value="${password}" />
    

      


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    设计模式12-享元模式
    设计模式11-外观模式
    设计模式10-装饰模式
    设计模式09-组合模式
    设计模式08-桥接模式
    设计模式07-适配器模式
    设计模式06-原型模式
    获取cookie信息
    JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
    jmeter基础概念
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2757553.html
Copyright © 2020-2023  润新知