Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置

1.项目文件分布。

Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置

 

2、example1.java:

package com.rhythmk.example1;



import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;



public class example1 {



	private static String resource = "com\\rhythmk\\example1\\SqlMapConfig.xml";

	private static SqlMapClient sqlMapClient = null;

	static {

		System.out.println(System.getProperty("user.dir"));

		try {

			Reader reader = Resources.getResourceAsReader(resource);

			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

			reader.close();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}



	public static void main(String[] args) {

		// TODO Auto-generated method stub



		GetAllUser();

		

		GetUserById(1);

	}



	/*

	 * 根据ID 获取User

	 */

	public static void GetUserById(Integer id) {

		User user = null;

		try {



			sqlMapClient.startTransaction();



			user = (User) sqlMapClient.queryForObject("selectById",

					new Integer("1"));

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

			sqlMapClient.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}



	public static void GetAllUser() {

		List<User> list = null;

		try {



			list = (List<User>) sqlMapClient.queryForList("selectAll");

			for (User user : list) {

				System.out.println(user.getId() + "," + user.getName());

			}



		} catch (Exception e) {

			e.printStackTrace();

		}



	}



	



}

  

3、User.java:

package com.rhythmk.example1;



public class User {



	 public int getId() {

		return Id;

	}

	public void setId(int id) {

		Id = id;

	}

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

	public int getAge() {

		return Age;

	}

	public void setAge(int age) {

		Age = age;

	}

	private int Id;

	 private String  name;

	 private int Age;

	

	 

}

  

4、SqlMap.properties:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/test

username=root

password=rhythmk

 

5、SqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig      

     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      

     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

 <sqlMapConfig>

     <!-- 数据库连接的属性文件 -->

     <properties resource="com\rhythmk\example1\SqlMap.properties" />

     

    

     <transactionManager type="JDBC"><!-- type:定义了ibatis的事务管理器有3种,JDBC、JTA、EXTERNAL -->

         <dataSource type="SIMPLE"><!-- type属性指定了数据源的连接类型有3种,SIMPLE、DBCP、JNDI -->

             <property value="${driver}" name="JDBC.Driver" />

             <property value="${url}" name="JDBC.ConnectionURL" />

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

             <property value="${password}" name="JDBC.Password" />

         </dataSource>

     </transactionManager>

     

     <!-- 实体类和数据库表的映射 -->

     <sqlMap resource="com\rhythmk\example1\User.xml" />

     

 </sqlMapConfig>

 

6.User.xml:

<?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE sqlMap      

     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      

     "http://ibatis.apache.org/dtd/sql-map-2.dtd">

 <sqlMap>

     <typeAlias alias="User" type="com.rhythmk.example1.User" />

  <!-- 查询所有记录  -->

     <select resultClass="User" id="selectAll">

         SELECT  id, name ,age   from  `user` 

     </select>



   <!-- 精确查询 按照条件查询记录  按照部门编号DEPTNO查询 -->

   <select parameterClass="int" resultClass="User" id="selectById">

         SELECT  id, name ,age   from  `user` where id=#id#

   </select>

</sqlMap>

SQL:

  

CREATE TABLE `user` (

`Id` int(11) NOT NULL,

`Name` varchar(32) CHARACTER SET utf8 NOT NULL,

`Age` int(11) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;



 

http://files.cnblogs.com/rhythmK/IbatisExample.rar

你可能感兴趣的:(ibatis)