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

jar包

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处理优化 (转)

    为什么要做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. 学习笔记_Java_day12_设计模式MVC(13).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+struts2+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. Round545div2B(1138B)

    一.题目链接 https://codeforces.com/problemset/problem/1138/B 二.思路 贪心是肯定不行的. 设会$[1,0]$的人存在容器$p_1$里面,会$[0,1 ...

  2. CentOS安装nginx以及负载均衡的搭建

    依赖环境,没有安装的需要安装一下 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install open ...

  3. 02-zip文件打包

    package com.day1; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...

  4. core 部署

    perfmoneventvwr 1.yum install mysql2.yum install libgdiplus-devel3.COMPlus_ThreadPool_ForceMinWorker ...

  5. JavaScript类继承, 用什么方法好

    JavaScript类继承, 用什么方法好 一个实例: 基类Car: function Car(color, year) { this.name = "car"; this.col ...

  6. Java 1-Java 基础语法

    一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一条狗是一个对象,它的 ...

  7. jQuery实现点击控制左右两边元素挤压显示效果

    该功能实现的是:分左.右两边布局,左边div默认展开,左边div中有一个元素,点击实现左边div隐藏,右边div挤压过来:再点击实现左边显示,右边挤过去. 一.HTML代码: <div clas ...

  8. Spring MVC开启注解

    1.在spring的配置文件中配置:<context:annotation-config />该项配置只能应用于已经注册的bean,比较难用,不深究. 2.在spring的配置文件中使用c ...

  9. nvm+nodejs+npm

    安装nvm 1.下载nvm包,地址:https://github.com/coreybutler/nvm-windows/releases选择nvm-setup.zip下载, 解压,双击安装即可 2. ...

  10. WPF开发ArcGis系统时的异常信息: ArcGIS product not specified. You must first bind to an ArcGIS version prior to using any ArcGIS components.

    “System.Runtime.InteropServices.COMException”类型的未经处理的异常在 Arcgis_Test.exe 中发生 其他信息: ArcGIS product no ...