IUserDAOImpl.java
- package com.elifefly;
- /*
- * 说明:程序是根据传智播客ibatis视频教程编写的入门例子。
- * 目的在于熟悉ibatis的基本原理和基本使用方法。
- * 因而程序并没有过多的开发成一个系统,ibatis视频教程也没有做成系统的,也没有给任何源代码。
- *
- * 开发环境:MyEclipse 6.0
- * 数据库:MySQL5.0
- *
- * 要添加的jar包:mysql-connector-java-3.1.10-bin.jar
- * 和ibatis-2.3.3.720压缩包下的lib文件夹下的ibatis-2.3.3.720.jar
- *
- * 测试使用,选中IUserDAOImpl.java文件,选择run as Java Application
- */
- import java.io.IOException;
- import java.io.Reader;
- import java.sql.SQLException;
- import java.util.List;
- import com.ibatis.sqlmap.client.SqlMapClient;
- public class IUserDAOImpl implements IUserDAO
- {
- private static SqlMapClient sqlMapClient = null;
- static
- {
- try
- {
- Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("com/elifefly/SqlMapConfig.xml");
- sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- } catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //添加用户
- public void addUser(User user)
- {
- try
- {
- sqlMapClient.insert("insertUser",user);
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //添加用户,获取自动增长的序列号,处理添加相同的userId情况
- public void addUserBySequence(User user)
- {
- try
- {
- sqlMapClient.insert("insertUserBySequence",user);
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //删除用户
- public void deleteUserById(int id)
- {
- try
- {
- System.out.println(sqlMapClient.delete("deleteUserById",102));
- //如果打印出来的值大于0,说明删除成功
- System.out.println("如果打印出来的值大于0,说明删除成功");
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //查找所有用户
- public List<User> queryAllUser()
- {
- List<User> userList = null;
- try
- {
- userList = sqlMapClient.queryForList("selectAllUser");
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return userList;
- }
- //通过id查找用户
- public User queryUserById(int id)
- {
- User user = null;
- try
- {
- user = (User)sqlMapClient.queryForObject("selectUserById",id);
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return user;
- }
- //模糊查询,通过用户名查找用户
- public List<User> queryUserByName(String name)
- {
- List<User> userList = null;
- try
- {
- userList = sqlMapClient.queryForList("selectUserByName",name);
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return userList;
- }
- //修改用户
- public void updateUser(User user2)
- {
- try
- {
- System.out.println(sqlMapClient.update("updateUserById",user2));
- //如果打印出来的值大于0,说明更新成功
- System.out.println("如果打印出来的值大于0,说明更新成功");
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- IUserDAOImpl iUserDAOImpl = new IUserDAOImpl();
- //1.测试queryAllUser()
- System.out.println("1.测试queryAllUser()");
- for(User user:iUserDAOImpl.queryAllUser())
- {
- System.out.println(user);
- }
- //2.测试queryUserById(int id)]
- System.out.println("/n2.测试queryUserById(int id)");
- System.out.println(iUserDAOImpl.queryUserById(1));
- //3.测试addUser(User user)
- System.out.println("/n3.测试addUser(User user)");
- User user = new User();
- user.setUserId(102);
- user.setUserName("elifefly");
- user.setUserPassword("123456");
- user.setUserFlag("超级管理员");
- iUserDAOImpl.addUser(user);
- System.out.println(iUserDAOImpl.queryUserById(102));
- //4.测试deleteUserById(int id)
- System.out.println("/n4.测试deleteUserById(int id)");
- iUserDAOImpl.deleteUserById(102);
- //5.测试updateUser(User user)
- System.out.println("/n5.测试updateUser(User user)");
- User user2 = new User();
- user2.setUserId(101);
- user2.setUserName("myelifefly");
- user2.setUserPassword("12345678");
- user2.setUserFlag("超级");
- iUserDAOImpl.updateUser(user2);
- System.out.println(iUserDAOImpl.queryUserById(101));
- //6.测试List<User> queryUserByName(String name)
- System.out.println("6.测试List<User> queryUserByName(String name)");
- for(User user3:iUserDAOImpl.queryUserByName("e"))
- {
- System.out.println(user3);
- }
- //7.测试addUserBySequence(User user)
- System.out.println("/n7.测试addUserBySequence(User user)");
- User user7 = new User();
- user7.setUserId(100);
- user7.setUserName("good");
- user7.setUserPassword("123456");
- user7.setUserFlag("test");
- iUserDAOImpl.addUserBySequence(user7);
- for(User user8:iUserDAOImpl.queryAllUser())
- {
- System.out.println(user8);
- }
- }
- }
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/elifefly/SqlMap.properties" />
- <!-- Configure a built-in transaction manager. If you're using an
- app server, you probably want to use its transaction manager
- and a managed datasource -->
- <transactionManager type="JDBC" >
- <dataSource type="SIMPLE">
- <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/elifefly/User.xml"/>
- <!-- List the SQL Map XML files. They can be loaded from the
- classpath, as they are here (com.domain.data...) -->
- <!-- List more here...
- <sqlMap resource="com/mydomain/data/Order.xml"/>
- <sqlMap resource="com/mydomain/data/Documents.xml"/>
- -->
- </sqlMapConfig>
SqlMap.properties
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/college
- username=root
- password=123456
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.elifefly.User" />
- <!-- 查找所有用户 -->
- <select id="selectAllUser" resultClass="User" >
- select * from user;
- </select>
- <!-- 通过id查找用户 -->
- <!-- parameterClass表示参数类型 -->
- <select id="selectUserById" parameterClass="int" resultClass="User">
- select * from user
- where userId = #userId#
- </select>
- <!-- 添加用户 -->
- <insert id="insertUser" parameterClass="User">
- insert into user(userId,userName,userPassword,userFlag)
- values(#userId#,#userName#,#userPassword#,#userFlag#);
- <!-- 注意这里的#userId#对应User.java中的get和set方法属性的,要与其对应,修改的时候要对应 -->
- </insert>
- <!-- 删除用户 -->
- <delete id="deleteUserById" parameterClass="int">
- delete from user
- where userId = #userId#
- <!-- 特别注意:这个delete中的 #userId#可以改为任意的其他如 #Id#等,而在其他如select等语句不可行 -->
- </delete>
- <!-- 修改用户 -->
- <update id="updateUserById" parameterClass="User">
- update user
- set userName = #userName#,
- userPassword = #userPassword#,
- userFlag = #userFlag#
- where userId = #userId#
- </update>
- <!-- 模糊查询,通过用户名查找用户 -->
- <select id="selectUserByName" parameterClass = "String" resultClass = "User">
- select userId,userName,userPassword,userFlag
- from user
- where userName like '%$userName$%'
- <!-- 非常注意:这个'%$userName$%'中的$符号和#的不同使用之处,$表示会用变量的只替换掉,用#是不行的,#是占位符 -->
- </select>
- <!-- 添加用户,获取自动增长的序列号,处理添加相同的userId情况 -->
- <insert id="insertUserBySequence" parameterClass = "User" >
- <selectKey resultClass = "int" keyProperty="userId" >
- SELECT @@IDENTITY as userId
- </selectKey>
- insert into user(userId,userName,userPassword,userFlag)
- values(#userId#,#userName#,#userPassword#,#userFlag#);
- </insert>
- </sqlMap>
User.java
- package com.elifefly;
- public class User
- {
- //注意必须保证有一个无参数方法,否则出错。
- //因为反射的时候调用的是无参数的方法
- private int userId = 0;
- private String userName = null;
- private String userPassword = null;
- private String userFlag = null;
- public int getUserId()
- {
- return userId;
- }
- public void setUserId(int userId)
- {
- this.userId = userId;
- }
- public String getUserName()
- {
- return userName;
- }
- public void setUserName(String userName)
- {
- this.userName = userName;
- }
- public String getUserPassword()
- {
- return userPassword;
- }
- public void setUserPassword(String userPassword)
- {
- this.userPassword = userPassword;
- }
- public String getUserFlag()
- {
- return userFlag;
- }
- public void setUserFlag(String userFlag)
- {
- this.userFlag = userFlag;
- }
- @Override
- public String toString()
- {
- //重载toString方法,方便调用
- String content = "userId=" + userId + "/tuserName=" + userName + "/tuserPassword=" +userPassword +"/tuserFlag=" +userFlag;
- return content;
- }
- }
IUserDAO.java
- package com.elifefly;
- import java.util.List;
- public interface IUserDAO
- {
- //增删改查
- public void addUser(User user);
- public void addUserBySequence(User user);
- public void deleteUserById(int id);
- public void updateUser(User user);
- public List<User> queryAllUser();
- public List<User> queryUserByName(String name);
- public User queryUserById(int id);
- }