JAVAWEB 一一ibatis(框架)

时间:2021-12-12 08:41:06

,升级版是mybatis,在配置文件里写sql语句对字段进行CURD)

jar包

JAVAWEB 一一ibatis(框架)

JAVAWEB 一一ibatis(框架)

sqlMapConfig

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig> <!--给较长的类取个别名-->
<!--是否启用缓存机制-->
<!-- 是否启用延迟加载机制 -->
<!-- 是否启用字节码增强机制 -->
<!-- 是否启用错误处理机制 -->
<!-- 最大并发请求数 -->
<!-- 最大Session数 -->
<!-- 最大并发事务数 -->
<!-- 是否启用名称空间 -->
<settings
cacheModelsEnabled ="true"
lazyLoadingEnabled="true"
enhancementEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"/>
<!-- 定义了ibatis的事务管理器有3中(JDBC,JTA,EXTERNAL) -->
<!-- type属性指定了数据源的链接类型,也有3种类型(SIMPLE,DBCP,JNDI) -->
<!-- 连接池维持的最大容量 -->
<!-- 连接池允许挂起的最大连接 -->
<!-- 连接被某个任务所允许占用的最大时间 -->
<!-- 线程允许等待的最大时间 -->
<transactionManager type ="JDBC" >
<dataSource type ="SIMPLE" >
<property name ="JDBC.Driver" value ="oracle.jdbc.OracleDriver" />
<property name ="JDBC.ConnectionURL" value ="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name ="JDBC.Username" value ="scott" />
<property name ="JDBC.Password" value ="orcl" />
<property name ="Pool.MaximumActiveConnections" value ="10" />
<property name ="Pool.MaximumIdleConnections" value ="5" />
<property name ="Pool.MaximumCheckoutTime" value ="120000" />
<property name ="TimeToWait" value ="500" />
</dataSource >
</transactionManager > <!--
<transactionManager type ="JDBC">
<dataSource type ="JNDI">
<property name ="dataSource" value ="java:comp/env/jdbc/emp" />
</dataSource>
</transactionManager>
-->
<sqlMap resource ="com/ibatis/entity/userMap.xml" />
</sqlMapConfig >

  userMap .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 namespace="user"> <typeAlias alias="User" type="com.ibatis.entity.User"/>
<parameterMap id="userParameterMap" class="User" >
<parameter property="username" />
<parameter property="userpassword" />
<parameter property="role" />
<parameter property="state" />
<parameter property="email" />
<parameter property="createdate" />
<parameter property="userid" />
</parameterMap> <resultMap id="userResultMap" class="User">
<result property="userid" column="USERID"/>
<result property="username" column="USERNAME"/>
<result property="userpassword" column="PASSWORD"/>
<result property="role" column="ROLE"/>
<result property="state" column="STATE"/>
<result property="email" column="EMAIL"/>
<result property="createdate" column="CREATEDATE"/>
</resultMap> <select id="getUserByName" resultClass="User" >
select * from USER_NEW where username =#username#
</select>
<insert id="insertUserInfo" parameterClass="User">
insert into USER_NEW values(#userid#,#username#,#userpassword#,#role#,#state#,#email#,#createdate#)
</insert>
<update id="updateUserInfo" parameterMap="userParameterMap">
update USER_NEW set
username=?,userpassword=?,role =? ,state =?,email=?,createdate=?
where userid =?
</update>
<delete id="deleteUserInfo" parameterClass="java.lang.Integer">
delete from USER_NEW WHERE userid=#userid#
</delete>
<select id="getUserList" resultClass="User">
select
USERID as userid,
USERNAME as username,
USERPASSWORD as userpassword,
ROLE as role,
STATE as state,
EMAIL as email,
CREATEDATE as createdate
FROM USER_NEW </select>
<select id="getUserMap" resultClass="java.util.HashMap">
select * from USER_NEW
</select> <select id="getUser" parameterClass="User" resultMap="userResultMap">
select*from USER_NEW
<dynamic prepend=" where ">
<isNotNull property="username" prepend="and"> username=#username#</isNotNull>
<isNotNull property="userpassword" prepend="and"> userpassword=#userpassword#</isNotNull>
</dynamic>
</select> <select id="getUser2" parameterClass="User" resultMap="userResultMap">
select*from USER_NEW where 1=1
<isNotNull property="username" prepend="and"> username=#username#</isNotNull>
<isNotNull property="userpassword" prepend="and"> userpassword=#userpassword#</isNotNull>
</select>
</sqlMap>

 User.java

  

package com.ibatis.entity;

import java.util.Date;

public class User {
private int userid;
private String username;
private String userpassword;
private int role;
private int state;
private String email;
private Date createdate; public User(String username, String userpassword, int role, int state,
String email, Date createdate) {
super();
this.username = username;
this.userpassword = userpassword;
this.role = role;
this.state = state;
this.email = email;
this.createdate = createdate;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
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 int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
} }

  

 UserDao.java

package com.ibatis.dao;

import java.util.List;
import java.util.Map; import com.ibatis.entity.User; public interface UserDao {
public User getUserByName(String username);
public boolean addUser(User user);
public boolean updateUser(User user);
public boolean delUser(int userid);
public List<User> exploreIbatis();
public Map<String,Object> exploreIbatis2(); public User getUser(User user);
}

  UserDaoImpl.java

package com.ibatis.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.ibatis.dao.UserDao;
import com.ibatis.entity.User;
import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.util.SqlMapClientUtil; public class UserDaoImpl implements UserDao { SqlMapClient sqlMapClient = SqlMapClientUtil.getSqlMapClient();
User user=null;
public User getUserByName(String username){
try {
//("user.getUserByName")命名空间 id username
user = (User)sqlMapClient.queryForObject("user.getUserByName", username);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
} public boolean addUser(User user) {
try {
int count=sqlMapClient.update("user.insertUserInfo", user);
System.out.println(count);
if(count>0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} public boolean updateUser(User user) {
try {
int count=sqlMapClient.update("user.updateUserInfo", user);
System.out.println(count);
if(count>0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} public boolean delUser(int userid) {
try {
int count =sqlMapClient.delete("user.deleteUserInfo", userid);
System.out.println(count);
if(count>0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public List<User> exploreIbatis(){
List<User> list= new ArrayList<User>();
try {
list = sqlMapClient.queryForList("user.getUserList"); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public Map<String,Object> exploreIbatis2(){
Map <String,Object>map = new HashMap<String, Object>();
try { map = sqlMapClient.queryForMap("user.getUserMap", null, "userid"); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
} public User getUser(User user) {
User userTemp=null;
try {
userTemp=(User)sqlMapClient.queryForObject("user.getUser", user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} }

  SqlMapClientUtil

package com.ibatis.util;

import java.io.IOException;
import java.io.Reader; import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class SqlMapClientUtil {
public static SqlMapClient getSqlMapClient() {
SqlMapClient sqlMapClient = null;
Reader reader = null; try {
reader = Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sqlMapClient; }
}

  SqlMapClientTest

package com.ibatis.util;

import java.io.IOException;
import java.io.Reader; import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class SqlMapClientUtil {
public static SqlMapClient getSqlMapClient() {
SqlMapClient sqlMapClient = null;
Reader reader = null; try {
reader = Resources.getResourceAsReader("sqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sqlMapClient; }
}

  

JAVAWEB 一一ibatis(框架)的更多相关文章

  1. iBatis框架batch处理优化 &lpar;转&rpar;

    为什么要做batch处理        这个问题我就不解释了,因为我想你们肯定能比我解释的更好!如果你真的不知道,那就到Google上去搜索一下吧☻Oracle回滚段    这个问题偶也不很明白,只是 ...

  2. iBatis框架基本使用

    iBatis框架是Java持久层开发框架,说白了就是前人写了一部分代码(针对数据库操作),我们要做的就是再次开发,拿来框架直接使用. 我们自己开发时,dao层的sql语句都是写死在程序中的,如果查询条 ...

  3. iBatis框架简介

    一.为啥使用iBatis? 在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解决方案盛行之前,iBaits 基本是持久层框架的不二选择.即使在持久层框架层出不穷 ...

  4. 【转】深入分析 iBATIS 框架之系统架构与映射原理

    深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...

  5. 学习笔记&lowbar;Java&lowbar;day12&lowbar;设计模式MVC(13)&period;JavaWeb的三层框架(14)

    MVC 1. 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Contr ...

  6. 深入分析 iBATIS 框架之系统架构与映射原理--转载

    http://www.ibm.com/developerworks/cn/java/j-lo-ibatis-principle/ iBATIS 通过 SQL Map 将 Java 对象映射成 SQL ...

  7. spring&plus;struts2&plus;ibatis 框架整合以及解析

    一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...

  8. spring struts2 ibatis 框架结构图

    spring struts2 ibatis 框架结构图

  9. 框架学习:ibatis框架的结构和分析

    由于最近一段时间比较忙碌,<框架学习>系列的文章一直在搁浅着,最近开始继续这个系列的文章更新. 在上篇文章中我们说到了hibernate框架,它是一种基于JDBC的主流持久化框架,是一个优 ...

随机推荐

  1. fping tcping hping nmap nc

    [root@test ~]# fping -a -g 192.168.40.1 192.168.40.240 |nl   #-a   扫描alive主机,-g扫描一个段的ip地址 [root@test ...

  2. 转:21副GIF动图让你了解各种数学概念

    21副GIF动图让你了解各种数学概念

  3. &lbrack;oracle&rsqb; ORACLE&lowbar;HOME&lowbar;LISTNER is not SET&comma; unable to auto-start Oracle Net Listener

    安装好Oracle数据库后: 执行 dbstart和dbshut会提示: [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, un ...

  4. 利用ffmpeg解码h264流的代码

    这里也直接给出代码: h264dec.h: #pragma once #include "tdll.h" #include "avcodec.h" #inclu ...

  5. Java多态的体现之继承

    /** * * 功能:演示多态 */ package com.litao; public class Demo5 { public static void main(String[] args) { ...

  6. Unity3D 之NGUI各种脚本及应用

    这里来介绍一下NGUI的各种脚本的作用,以便以后需要某种效果的时候,去添加相应的脚本去实现效果 UIButton --> 按钮脚本 UIPanel   --> 面板脚本 UIToggle  ...

  7. spoolsv&period;exe 报错,无法打印

    在使用打印机过程中突然出现spoolsv.exe应用程序错误,内存不能written•••,检查打印驱动,打印机设置选项无法打开.怀疑是病毒所致,升级杀毒软件后安全模式下杀毒后没有发现病毒,重启后还是 ...

  8. Shell编程之函数调用

    Shell中的函数调用的使用方法见我下面的代码示例: #!/bin/bash # value init ExP="adb shell /data/local/tmp/vpxdec --yv1 ...

  9. flat ui switch 改变状态而不响应事件

    Flat UI是一套精美的扁平风格 UI 工具包,基于 Twitter Bootstrap实现.这套界面工具包含许多基本的和复杂的 UI 部件,例如按钮,输入框,组合按钮,复选框,单选按钮,标签,菜单 ...

  10. django 基础入门(二)

    一.关于数据库 1.首先django 1.9以上等版本不支持pymysql,因此需要做一些调整. 比如在settings.py 加入一段代码: import pymysql pymysql.insta ...