ibatis练习小程序
学了一下ibatis,自己写了一个很小很小的程序,权当练习一下。
1. 建数据库
在Mysql里面建了个数据库以及person和role表
create database ibatis;
create table person(
id varchar(20) primary key,
name varchar(20),
pwd varchar(20),
role_id varchar (20) foreign key references role(id)
);
create table role(
id varchar(20) primary key,
role_name varchar(20),
role_desc varchar(20)
);
2.建web project LearnIbatis
在myEclipse中创建web project LearnIbatis
新建Person类
package com.hundsun.domain;
public class Person {
private String id;
private String name;
private String pwd;
private Role role;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Role类:
package com.hundsun.domain;
public class Role {
private String id;
private String roleName;
private String roleDesc;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
}
3. 配置java bean和数据库表之间的对应关系(路径:com/hundsun/ibatis/)
personSql.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 namespace="Person" >
<typeAlias alias= "Person" type="com.hundsun.domain.Person" />
<resultMap id="personResult">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="pwd"/>
<result column="roleId" property="role" select="Role.getRoleById"/>
</resultMap>
<select id="getPersonById" resultMap="personResult">
<![CDATA[
select * from person where id = #id#
]]>
</select>
<select id="getAllPerson" resultMap="personResult">
<![CDATA[
select * from person
]]>
</select>
<insert id="addPerson" parameterencoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Role">
<typeAlias alias="Role" type="com.hundsun.domain.Role"/>
<resultMap id="roleResult">
<result column="id" property="id" />
<result column="role_name" property="roleName" />
<result column="role_desc" property="roleDesc" />
</resultMap>
<select id="getRoleById" resultMap="roleResult">
<![CDATA[
select * from role where id=#id#
]]>
</select>
</sqlMap>
4. 配置sql_config_map.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/hundsun/ibatis/learnIbatisSql.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="128"
maxSessions="64"
maxTransactions="5"
useStatementNamespaces="true"
/>
<transactionManager type="JDBC">
<dataSource type="Simple">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/hundsun/ibatis/personSql.xml"/>
<sqlMap resource="com/hundsun/ibatis/roleSql.xml"/>
</sqlMapConfig>
5. 新建JDBC属性文件
learnIbatisSql.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ibatis
username=root
password=root
6. 新建main方法
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.hundsun.domain.Person;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class learnIbatis {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SqlMapClient sqlMap = null;
Reader reader = Resources.getResourceAsReader("com/hundsun/ibatis/sql_config_map.xml");
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List<Person> personList = sqlMap.queryForList("Person.getPersonById", 1);
List<Person> allPersonList = sqlMap.queryForList("Person.getAllPerson");
System.out.println(sqlMap.getDataSource());
System.out.println(sqlMap.getCurrentConnection());
for (int i=0; i < personList.size(); i++){
System.out.println(personList.get(i).getId());
System.out.println(personList.get(i).getName());
System.out.println(allPersonList.get(i).getPwd());
System.out.println("role" + allPersonList.get(i).getRole());
}
for (int i=0; i < allPersonList.size(); i++){
System.out.println(allPersonList.get(i).getId());
System.out.println(allPersonList.get(i).getName());
System.out.println(allPersonList.get(i).getPwd());
System.out.println("role name: " + allPersonList.get(i).getRole().getRoleName());
}
}
}
7.在数据库里输入一些数据
8.运行,可以获取数据库中的结果并输出。