spring和ibatis综合应用入门示例
程序员文章站
2024-01-24 11:34:40
...
1、最近几天在学习ibatis和spring,就把两者综合起来,写了示例。
2、构建数据库表user,使用数据库是mysql;
2、根据数据库表生成相应的JavaBean
3、对应JavaBean的配置文件User.xml文件:
4、ibatis和spring的配置文件
ibatis的配置sqlMapConfig.xml文件:
spring的配置applicationContext.xml文件:
5、编写相应的类
UserDao.java
IUserDao.java
TestIUserDao.java
6、项目的包结构
7、程序结构如下:
2、构建数据库表user,使用数据库是mysql;
create table user( id int(10) not null auto_increment, userName varchar(20) , passWord varchar(20), primary key(id) ); insert into user(userName,passWord) values('admin1','admin1'); insert into user(userName,passWord) values('admin2','admin2'); insert into user(userName,passWord) values('admin3','admin3'); insert into user(userName,passWord) values('admin4','admin4');
2、根据数据库表生成相应的JavaBean
package com.tonyj.bean; /** * @author tonyJ * @date 2012-3-17 下午01:00:25 */ public class User { private int id; private String userName; private String passWord; //相应的getter和setter方法,构造方法 }
3、对应JavaBean的配置文件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.tonyj.bean.User"/> <resultMap class="User" id="userResultMap"> <result property="id" column="id"/> <result property="userName" column="userName"/> <result property="passWord" column="passWord"/> </resultMap> <!-- 获得全查询列表 --> <select id="getAllUsers" resultMap="userResultMap"> select * from user </select> <!-- 根据用户名查询用户对象 --> <select id="getUserByName" resultMap="userResultMap" parameterClass="String"> select * from user where userName=#value# </select> <!-- 根据用户id查询用户对象 --> <select id="getUserById" resultMap="userResultMap" parameterClass="Integer"> select * from user where id=#value# </select> <!--新增用户对象 --> <insert id="insertUser" parameterClass="User"> insert into user(userName,passWord) values(#userName#,#passWord#) </insert> <!-- 删除用户对象 --> <delete id="deleteUser" parameterClass="Integer"> delete from users where id=#id# </delete> <!-- 更新用户对象 --> <update id="updateUser" parameterClass="User"> update user set userName=#userName#,passWord=#passWord# where id=#id# </update> </sqlMap>
4、ibatis和spring的配置文件
ibatis的配置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> <sqlMap resource="User.xml"/> </sqlMapConfig>
spring的配置applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>sa</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/vin</value> </property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” --> <property name="configLocation" value="SqlMapConfig.xml"/> </bean> <bean id="IUserDao" class="com.tonyj.dao.impl.IUserDao"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> </beans>
5、编写相应的类
UserDao.java
package com.tonyj.dao; import java.util.List; import com.tonyj.bean.User; /** * @author tonyJ * @date 2012-3-17 下午12:59:22 */ public interface UserDao { public List<User> getUserList(); public User getUserByName(String userName); public User getUserById(int id); public void saveUser(User user); public int updateUser(User user); public int deleteUser(int id); }
IUserDao.java
package com.tonyj.dao.impl; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.tonyj.bean.User; import com.tonyj.dao.UserDao; /** * @author tonyJ * @date 2012-3-17 下午01:04:23 */ public class IUserDao extends SqlMapClientDaoSupport implements UserDao{ @SuppressWarnings("unchecked") @Override public List<User> getUserList() { return getSqlMapClientTemplate().queryForList("getAllUsers"); } @Override public User getUserByName(String userName) { return (User) getSqlMapClientTemplate().queryForObject("getUserByName", userName); } @Override public User getUserById(int id) { return (User) getSqlMapClientTemplate().queryForObject("getUserById", id); } @Override public void saveUser(User user) { getSqlMapClientTemplate().insert("insertUser", user); } @Override public int updateUser(User user) { return getSqlMapClientTemplate().update("updateUser",user); } @Override public int deleteUser(int id) { return getSqlMapClientTemplate().delete("deleteUser", id); } }
TestIUserDao.java
package com.tonyj.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tonyj.bean.User; import com.tonyj.dao.impl.IUserDao; /** * @author tonyJ * @date 2012-3-17 下午01:37:19 */ public class TestIUserDao { public static void main(String[] args) { ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml"); IUserDao iud=(IUserDao) act.getBean("IUserDao"); //1、取得所有的对象 List<User> allUsers=new ArrayList<User>(); allUsers=iud.getUserList(); for(Iterator<User> iter=allUsers.iterator();iter.hasNext();){ User users=iter.next(); System.out.println(users.getId()+"-->"+users.getUserName()+"-->"+users.getPassWord()); } //2、根据用户名查询 User getUserByName=new User(); getUserByName=iud.getUserByName("admin1"); System.out.println("根据用户名查询->"+getUserByName.getId()+"-->"+getUserByName.getPassWord()); //3、根据用户id查询 User getUserById=new User(); getUserById=iud.getUserById(1); System.out.println("根据用户id查询"+getUserById.getUserName()+"-->"+getUserById.getPassWord()); } }
6、项目的包结构
7、程序结构如下:
2012-3-20 19:49:08 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b4333: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]; startup date [Tue Mar 20 19:49:08 CST 2012]; root of context hierarchy 2012-3-20 19:49:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 2012-3-20 19:49:09 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@4b4333]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1632c2d 2012-3-20 19:49:09 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1632c2d: defining beans [dataSource,sqlMapClient,IUserDao]; root of factory hierarchy 1-->admin1-->admin1 2-->admin2-->admin2 3-->admin3-->admin3 4-->admin4-->admin4 根据用户名查询->1-->admin1 根据用户id查询admin1-->admin1