• MyBatis入门学习(一)


    所须要jar包:mybatis-3.x.x.jar 、假设须要和spring整合,还须要添�相关的包

    1:看项目文件夹 红颜色不要关心

    2:依照步骤:

    1:添�jar包

    2:创建数据源(configuration.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>
    		<!--给实体类起一个别名 user -->
    		<typeAlias type="com.forum.po.User" alias="User" />
    	</typeAliases>
    	<!--数据源配置  这块用 BD2数据库 -->
    	<environments default="development">
    		<environment id="development">
    			<transactionManager type="jdbc" />
    			<dataSource type="POOLED">
    				<property name="driver" value="com.ibm.db2.jcc.DB2Driver" />
    				<property name="url" value="jdbc:db2://localhost:50000/forum" />
    				<property name="username" value="DB2ADMIN" />
    				<property name="password" value="admin" />
    			</dataSource>
    		</environment>
    	</environments>
    	<mappers>
    		<!--userMapper.xml装载进来  同等于把“dao”的实现装载进来 -->
    		<mapper resource="myBatis/userMapper.xml" />
    	</mappers>
    </configuration> 
    3:创建实体类USER

    package com.forum.po;
    
    /**
     * 用户类
     * 
     * @author db2admin
     * 
     */
    public class User extends Entity {
    	private String name;
    	private Integer age;
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	public User() {
    	}
    
    }

    4:创建dao接口、能够说是Mapper接口:UserMapper

    package com.forum.dao;
    
    import com.forum.po.User;
    
    public interface UserMapper {
    	public User findById(String Id);
    }
    

    5:创建dao的实现,不同于hibernarte的此处的实现为一个xml文件,也就是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">
    	<!--这块等于dao接口的实现  namespace必须和接口的类路径一样-->
    <mapper namespace="com.forum.dao.UserMapper">
    	<!-- findById必须和接口中的方法名一样  返回一个User  就是刚才的别名  假设不弄别名要连类路径一起写 麻烦-->
    	<select id="findById" parameterType="HashMap" resultType="User">
    		select
    		* from butone.student where id=#{id}
        </select>
    </mapper>

    6:測试类,MyBatisTest

    package com.forum.test;
    
    import java.io.IOException;
    
    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.forum.dao.UserMapper;
    import com.forum.po.User;
    
    /**
     * myBatis数据库连接測试
     * 
     * @author db2admin
     * 
     */
    public class MyBatisTest {
    	/**
    	 * 获得MyBatis SqlSessionFactory  
    	 * SqlSessionFactory负责创建SqlSession,一旦创建成功,就能够用SqlSession实例来运行映射语句,commit,rollback,close等方法。
    	 * @return
    	 */
    	private static SqlSessionFactory getSessionFactory() {
    		SqlSessionFactory sessionFactory = null;
    		String resource = "configuration.xml";
    		try {
    			sessionFactory = new SqlSessionFactoryBuilder().build(Resources
    					.getResourceAsReader(resource));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return sessionFactory;
    	}
    
    	public static void main(String[] args) {
    		SqlSession sqlSession = getSessionFactory().openSession();
    		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    		User user = userMapper.findById("1");
    		System.out.println(user.getName());
    
    	}
    
    }

  • 相关阅读:
    H3C BGP配置10BGP安全功能典型配置举例
    H3C BGP配置9调整和优化BGP网络典型配置举例1BGP负载分担配置
    H3C BGP配置11 BGP网络的可靠性典型配置举例1BGP GR配置
    H3C BGP配置9调整和优化BGP网络典型配置举例2BGP AddPath配置
    vue移动端适配postcsspxtorem
    .net 技术站点(转载)
    邯郸.net俱乐部
    存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX 转载
    gridview中删除记录的处理
    邯郸.NET俱乐部正式成立了
  • 原文地址:https://www.cnblogs.com/blfshiye/p/3789297.html
Copyright © 2020-2023  润新知