最近2天在学ibatis,心里也有一些心得,就把它写下来了。
首先是配置一下ibatis的环境,添加ibatis2.X.jar,mysql-connection-bin.5.1.8.jar,建立一个web项目:
0、写个类User.jsva 有属性:.....(自己写),建个表:user.sql
1、先建一个数据库文件dataSource.properties,内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
2、这是ibatis的核心文件:sqlmap-config.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="dataSource.properties"></properties>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${jdbc.driver}" />
<property name="JDBC.ConnectionURL" value="${jdbc.url}" />
<property name="JDBC.Username" value="${jdbc.username}" />
<property name="JDBC.Password" value="${jdbc.password}" ></property>
</dataSource>
</transactionManager>
<sqlMap resource="com/resource/User.xml" />
</sqlMapConfig>
3、建立一个user.xml文件,该文件里面写的是sql语句,这就是ibatis比hibernate查询数据更灵活的优势所在,简单的CRUD:
<?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.bean.User" />
<!-- 查询所有 -->
<select id="getAllUsers" resultClass="User">
select *
from user
</select>
<!-- 添加 -->
<insert id="saveUser" parameterClass="User">
insert into user (name,password,sex,address,phone,email)
values
(#name#,#password#,#sex#,#address#,#phone#,#email#)
<selectKey resultClass="int" keyProperty="id">
select last_insert_id()
</selectKey>
</insert>
<!-- 根据ID查询 -->
<select id="getById" parameterClass="int" resultClass="User">
select * from user u where u.id = #id#
</select>
<!-- 删除 -->
<delete id="deleteUser" parameterClass="int">
delete from user where id = #id#
</delete>
<update id="updateById" parameterClass="User">
update user u set u.name=#name#,u.password=#password#,u.sex=#sex#,u.address=#address#,u.phone=#phone#,u.email=#email#
where u.id=#id#
</update>
</sqlMap>
4、最后是测试。写个main()方法,测试一下是否搭建成功
public class IbatisDemo {
public static void main(String[] args) throws IOException, Exception {
// list();
// User user = new User();
// user.setName("ibatis");
// user.setPassword("123");
// user.setSex("男");
// user.setAddress("广州天河");
// user.setPhone("1324567");
// user.setEmail("123@qq.com");
// add(user);
// get(24);
// User user = new User();
// user.setName("ibatis2");
// user.setPassword("1232");
// user.setSex("男");
// user.setAddress("广州天河2");
// user.setPhone("13245672");
// user.setEmail("123@qq.com2");
// user.setId(23);
// update(user);
// delete1(23);
}
public static void list() throws IOException, SQLException{
Reader reader = Resources.getResourceAsReader("sqlmap-config-user.xml");
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
List<User> list = sqlMapClient.queryForList("getAllUsers");
for(User user : list){
System.out.println(user);
}
}
public static void add(User user) throws IOException, SQLException{
Reader reader = Resources.getResourceAsReader("sqlmap-config-user.xml");
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMapClient.insert("saveUser",user);
}
public static void get(Integer id) throws IOException, SQLException{
Reader reader = Resources.getResourceAsReader("sqlmap-config-user.xml");
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
//可以实现 方法一:
//List<User> list = sqlMapClient.queryForList("getById",id);
//for(User user:list){
// System.out.println("----------------"+list.size());
//}
//方法2:
User user0 = (User) sqlMapClient.queryForObject("getById",id);
System.out.println("----------------"+user0.getName());
}
public static void delete1(int id) throws IOException, SQLException{
Reader reader = Resources.getResourceAsReader("sqlmap-config-user.xml");
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMapClient.delete("deleteUser", id);
}
public static void update(User user) throws IOException, SQLException{
Reader reader = Resources.getResourceAsReader("sqlmap-config-user.xml");
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMapClient.update("updateById", user);
}
}