1. 环境:
将以下jar包加入到工程,commons-logging-1.0.4.jar、ibatis-2.3.0.677.jar、mysql-connector-java-5.0.3-bin.jar、spring.jar。
2. 在MySql中创建数据库和相应的表:
- #############################################################################################
- CREATE DATABASE MYDB;
- use MYDB;
- Drop TABLE IF EXISTS `MYDB`.`student`;
- Create TABLE `MYDB`.`student` (
- `name` varchar(40) NOT NULL,
- `psw` varchar(10) NOT NULL,
- `enabled` boolean
- );
- insert into student values("lanp","lanpiao",true);
- insert into student values("ph","ph",true);
- insert into student values("wxh","wxh",true);
- #############################################################################################
- CREATE DATABASE MYDB;
- use MYDB;
- Drop TABLE IF EXISTS `MYDB`.`student`;
- Create TABLE `MYDB`.`student` (
- `name` varchar(40) NOT NULL,
- `psw` varchar(10) NOT NULL,
- `enabled` boolean
- );
- insert into student values("lanp","lanpiao",true);
- insert into student values("ph","ph",true);
- insert into student values("wxh","wxh",true);
3. 创建实体Bean,Student.java
- package com.lanp.beans;
- import java.io.Serializable;
- /**
- * Student Bean
- * @author LanP
- * @since 2011-11-27 15:36
- * @version V1.0
- */
- public class Student implements Serializable {
- private static final long serialVersionUID = -7163004163334815825L;
- private String name;
- private String psw;
- private Boolean enabled;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPsw() {
- return psw;
- }
- public void setPsw(String psw) {
- this.psw = psw;
- }
- public Boolean getEnabled() {
- return enabled;
- }
- public void setEnabled(Boolean enabled) {
- this.enabled = enabled;
- }
- }
- package com.lanp.beans;
- import java.io.Serializable;
- /**
- * Student Bean
- * @author LanP
- * @since 2011-11-27 15:36
- * @version V1.0
- */
- public class Student implements Serializable {
- private static final long serialVersionUID = -7163004163334815825L;
- private String name;
- private String psw;
- private Boolean enabled;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPsw() {
- return psw;
- }
- public void setPsw(String psw) {
- this.psw = psw;
- }
- public Boolean getEnabled() {
- return enabled;
- }
- public void setEnabled(Boolean enabled) {
- this.enabled = enabled;
- }
- }
4. 创建Student实体Bean与数据库映射的SQLMap文件,student.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>
- <!-- 为Person类设置一个别名 -->
- <typeAlias alias="student" type="com.lanp.beans.Student"/>
- <!-- 配置表和实体Bean之间的映射关系 -->
- <resultMap id="studentMap" class="com.lanp.beans.Student">
- <result property="name" column="name"/>
- <result property="psw" column="psw"/>
- <result property="enabled" column="enabled"/>
- </resultMap>
- <insert id="insertStudent" parameterClass="student">
- <![CDATA[
- insert into student values(#name#,#psw#,#enabled#);
- ]]>
- </insert>
- <!-- 查看特定用户 -->
- <select id="queryStudentById" parameterClass="string" resultMap="studentMap">
- <![CDATA[
- SELECT * FROM STUDENT WHERE NAME=#name#
- ]]>
- </select>
- <!-- 查看所有的用户 -->
- <select id="queryAllStudents" resultMap="studentMap">
- <![CDATA[
- SELECT * FROM STUDENT
- ]]>
- </select>
- </sqlMap>
- <?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>
- <!-- 为Person类设置一个别名 -->
- <typeAlias alias="student" type="com.lanp.beans.Student"/>
- <!-- 配置表和实体Bean之间的映射关系 -->
- <resultMap id="studentMap" class="com.lanp.beans.Student">
- <result property="name" column="name"/>
- <result property="psw" column="psw"/>
- <result property="enabled" column="enabled"/>
- </resultMap>
- <insert id="insertStudent" parameterClass="student">
- <![CDATA[
- insert into student values(#name#,#psw#,#enabled#);
- ]]>
- </insert>
- <!-- 查看特定用户 -->
- <select id="queryStudentById" parameterClass="string" resultMap="studentMap">
- <![CDATA[
- SELECT * FROM STUDENT WHERE NAME=#name#
- ]]>
- </select>
- <!-- 查看所有的用户 -->
- <select id="queryAllStudents" resultMap="studentMap">
- <![CDATA[
- SELECT * FROM STUDENT
- ]]>
- </select>
- </sqlMap>
5. 创建访问数据库的DAO接口,StudentDao.java:
- package com.lanp.dao;
- import com.lanp.beans.Student;
- public interface StudentDao {
- Student getStudent(String name);
- }
- package com.lanp.dao;
- import com.lanp.beans.Student;
- public interface StudentDao {
- Student getStudent(String name);
- }
6. 创建访问数据库的DAO接口实现类,StudentDaoImpl.java:
- package com.lanp.dao;
- import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- import com.lanp.beans.Student;
- public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {
- @Override
- public Student getStudent(String name) {
- try{
- return (Student)getSqlMapClientTemplate().queryForObject("queryStudentById", name);
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- package com.lanp.dao;
- import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- import com.lanp.beans.Student;
- public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {
- @Override
- public Student getStudent(String name) {
- try{
- return (Student)getSqlMapClientTemplate().queryForObject("queryStudentById", name);
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
7. 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>
- <!-- 配置Ibatis要使用的SqlMap文件信息 -->
- <sqlMap resource="com/lanp/beans/student.xml"/>
- </sqlMapConfig>
- <?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>
- <!-- 配置Ibatis要使用的SqlMap文件信息 -->
- <sqlMap resource="com/lanp/beans/student.xml"/>
- </sqlMapConfig>
8. spring配置文件,SQLMapClient.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.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/>
- <property name="username" value="root"/>
- <property name="password" value="157891"/>
- </bean>
- <bean id="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="configLocation">
- <value>sqlMapConfig.xml</value>
- </property>
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl">
- <property name="sqlMapClient">
- <ref bean="sqlMapClient"/>
- </property>
- </bean>
- </beans>
- <?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.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/MYDB"/>
- <property name="username" value="root"/>
- <property name="password" value="157891"/>
- </bean>
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="configLocation">
- <value>sqlMapConfig.xml</value>
- </property>
- <property name="dataSource">
- <ref bean="dataSource"/>
- </property>
- </bean>
- <bean id="studentDao" class="com.lanp.dao.StudentDaoImpl">
- <property name="sqlMapClient">
- <ref bean="sqlMapClient"/>
- </property>
- </bean>
- </beans>
9. 测试类,TestStudent:
- package com.lanp.beans;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.lanp.dao.StudentDao;
- /**
- * 测试Ibatis
- * @author LanP
- * @since 2011-11-27 15:36
- * @version V1.0
- */
- public class TestStudent {
- public static void main(String[] args) {
- //1.初始化beans.xml文件
- ApplicationContext ctx = new ClassPathXmlApplicationContext("SQLMapClient.xml");
- //2.获取MYDB数据库Student表中的内容
- StudentDao studentDao = (StudentDao)ctx.getBean("studentDao");
- if(null != studentDao) {
- Student student = studentDao.getStudent("ph");
- if(null != student) {
- System.out.println("== 学生名字:" + student.getName() + ",学生密码:" + student.getPsw());
- } else {
- System.out.println("== 没有该学生信息!");
- }
- } else {
- System.out.println("== StudentDao注入失败!");
- }
- }
- }
原文地址:http://blog.csdn.net/lanpiao_87/article/details/7036409
相关推荐
《基于Spring+Struts+Ibatis的jPetStore实战详解》 jPetStore是一个经典的开源项目,它展示了如何使用Spring、Struts和Ibatis等技术构建一个完整的MVC(Model-View-Controller)架构的Web应用程序。这个项目是Java...
《Ibatis与MySQL整合详解:从入门到精通》 Ibatis,一款优秀的Java持久层框架,以其轻量级、灵活性和高效性深受开发者喜爱。它将SQL语句与Java代码分离,提供了更直观的数据库操作方式。而MySQL,作为世界上最受...
在本文中,我们将深入探讨如何将SpringBoot与MyBatis Plus进行整合,以便构建一个高效、简洁的...在实际应用中,你还可以结合其他SpringBoot特性,如自动配置、AOP、Spring Security等,进一步提升应用的功能和安全性。
# iBATIS 简单实例详解 iBATIS 是一款优秀的开源持久层框架,它为Java应用程序提供了灵活的数据访问接口。本篇文章将带你快速上手iBATIS,理解其配置、基础语义以及高级特性,并通过一个简单的实例来演示其工作流程...
**Ibatis jar包详解** Ibatis 是一个优秀的Java持久层框架,它主要负责数据库操作,提供了灵活的SQL映射机制,让开发者能够更方便地控制SQL的编写与执行。Ibatis 不是完整的ORM(对象关系映射)框架,而是简化了...
### Ibatis配置详解 1. **全局配置文件(ibatis-config.xml)** 全局配置文件是Ibatis系统的起点,它包含了数据源、事务管理器、插件、类型别名等整体设置。例如: ```xml <!DOCTYPE configuration PUBLIC "-/...
使用Spring框架时,可以使用`@Autowired`注解自动注入`UserMapper`实例。 ### 5. 简单案例 假设我们有一个简单的用户注册功能,可以通过以下步骤实现: 1. 创建`User`实体类。 2. 在`UserMapper.xml`中编写插入...
**Ibatis和Java集成详解** Ibatis,作为一个轻量级的持久层框架,与Java的集成使得数据库操作变得更加简单和灵活。它不同于传统的ORM(对象关系映射)框架如Hibernate,Ibatis更注重SQL的控制权,允许开发者编写...
**Ibatis 入门例子详解** Ibatis 是一款优秀的、轻量级的Java持久层框架,它主要解决了Java应用程序与数据库交互的问题。本教程将通过一个基础的Ibatis入门示例,帮助你理解如何在实际项目中使用Ibatis进行数据操作...
**Ibatis 入门代码详解** Ibatis 是一个优秀的开源持久层框架,它允许开发者将SQL语句直接写在配置文件中,与Java代码进行分离,从而降低了数据库操作的复杂性,提高了开发效率。本教程将带你一步步了解并掌握...
- 需要注意的是,这里使用了Spring框架的注解,如果你的项目中没有引入Spring,可以手动创建`SqlSession`和`SqlSessionFactory`实例。 7. **测试** - 创建一个`UserTest`类,使用JUnit或Spock等测试框架来测试`...
本篇文章将详细解析MyBatis的“Hello World”实例,帮助初学者快速入门。 【描述】"初始的mybatis的helloworld文档" MyBatis的“Hello World”教程是学习其基本概念和配置的绝佳起点。通过这个简单的例子,我们...
【初识MyBatis——实现增删改查详解】 MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解...
ant配置文件实例详解 build.xml 代码 xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程的名字;default:需要的缺省任务(运行"ant"不指明任务时执行的任务) --> by duzn 2007-04-02 回复 (0) Antenna与j2me...
【Java工程师面试知识点详解】 1. **项目经验与开发流程**:面试时,面试官可能会询问你在公司的任职时间,参与的项目以及项目的意义。你需要准备关于项目简介、目标、你在项目中的角色,以及项目开发流程的详细...