Spring + Ibatis + MySql实例详解
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);
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;
- }
- }
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>
5. 创建访问数据库的DAO接口,StudentDao.java:
- 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;
- }
- }
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>
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>
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注入失败!");
- }
- }
- }
OK,TKS!
Spring + Ibatis + MySql实例详解相关推荐
- ibatis mysql 配置文件详解_MyBatis Generator 配置文件详解
MyBatis Generator (MBG) 是由一个XML配置文件驱动的.这个配置文件中会声明以下内容: 如何连接数据库 要生成什么对象,以及如何生成它们 哪些表需要应用于对象生成. 根元素 ge ...
- linux mysql 实例详解_MySQL 多实例详解
目录 一.基本概念 1.MySQL多实例 就是在一台机器上面开启多个不同的端口,运行多个MySQL服务进程.这些MySQL多实例公用一套安装程序,使用不同的(也可以是相同的)配置文件,启动程序,数据文 ...
- mysql实例详解_MySQL 多实例详解
MySQL多实例配置方法 1.单一配置文件 2.多配置文件. 二.实战步骤: 1.同步时间 2.准备mysql依赖包 3.环境准备 3.1添加mysql用户 3.2编译安装mysql 4.安装MySQ ...
- connection url mysql,JDBC 连接MySQL实例详解
JDBC连接MySQL JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forNam ...
- ibatis mysql 配置文件详解_MyBatis配置文件详解
Mybatis配置文件 /p> PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dt ...
- 微信小程序form表单提交到MYSQL实例详解(PHP)
1.小程序相对于之前的WEB+PHP建站来说,个人理解为只是将web放到了微信端,用小程序固定的格式前前端进行布局.事件触发和数据的输送和读取,服务器端可以用任何后端语言写,但是所有的数据都要以JSO ...
- mysql的json函数与实例_Mysql实例详解Mysql中的JSON系列操作函数
<Mysql实例详解Mysql中的JSON系列操作函数>要点: 本文介绍了Mysql实例详解Mysql中的JSON系列操作函数,希望对您有用.如果有疑问,可以联系我们. MYSQL必读前言 ...
- java stopwatch_Spring StopWatch使用实例详解
这篇文章主要介绍了Spring StopWatch使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 StopWatch简单的秒表,允许多个 ...
- spring框架使用Quartz执行定时任务实例详解
版权声明:本文为博主原创文章,如需转载,请标明出处. https://blog.csdn.net/alan_liuyue/article/details/80382324 Quartz简介 1.Qua ...
最新文章
- C++、嵌入式软开之数据结构
- Redis生产环境架构选型方案对比
- mysql更改可执行文件路径_Mysql 服务 1067 错误 的解决方法:修改mysql可执行文件路径...
- Python-EEG工具库MNE中文教程(1)-MNE中数据结构Raw及其用法简介
- 16道嵌入式C语言面试题
- 【Java Web开发指南】redis笔记
- IDEA系列(十)--新建一个项目后之前的项目不显示
- 小白入门深度学习 | 第五篇:数据不均衡的处理方法
- 错误代码中文查询GetLastError返回代码的含义
- VideoLAN,VLC和FFmpeg社区联合开发AV1解码器
- redhat mysql编译安装_RHEL6.2编译安装MySQL 5.6.16过程分享
- 在c语言程序中 对文件进行操作首先要,《C语言程序设计》试题八及答案
- 卢伟冰正式预告Redmi K30:首发双开孔全面屏+双模5G
- javascript、jquery获取网页的高度和宽度
- reply-code=404, reply-text=NOT_FOUND - no exchange
- JAVA消息中间件面试题
- 深入浅出设计模式 ------ Prototype(原型模式)之深度克隆
- 从开发应用的角度来看,物联网的关键技术包括哪几个方面?
- 解决 adb no permissions (user mi is not in the plugdev group);
- 简转繁等中文转换(Golang)