- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章ibatis学习之搭建Java项目由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
IBATIS简介 。
ibatis是 Apache的开源项目,一个ORM 解决方案,ibatis最大的特点就是小巧,上手很快.
使用 ibatis提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的Java对象,这一层与通过Hibernate 实现ORM而言是基本一致的.
iBatis是一个基于SQL映射支持Java和·NET的持久层框架,相对Hibernate和ApacheOJB等“一站式”ORM解决方案而言,iBatis 是一种“半自动化”的ORM实现.
1、JAR包依赖 。
ibatis-2.3.4.726.jar 。
mysql-connector-java-5.0.8-bin.jar 。
2、SqlMap.properties 。
1
2
3
4
|
driver
=
com.mysql.jdbc.Driver
url
=
jdbc:mysql:
/
/
127.0
.
0.1
:
3306
/
test
username
=
root
password
=
root
|
3、SqlMapConfig.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?
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
>
<!-- 引用JDBC属性的配置文件 -->
<
properties
resource
=
"com/ligang/SqlMap.properties"
/>
<!-- 使用JDBC的事务管理 -->
<
transactionManager
type
=
"JDBC"
>
<!-- 数据源 -->
<
dataSource
type
=
"SIMPLE"
>
<
property
name
=
"JDBC.Driver"
value
=
"${driver}"
/>
<
property
name
=
"JDBC.ConnectionURL"
value
=
"${url}"
/>
<
property
name
=
"JDBC.Username"
value
=
"${username}"
/>
<
property
name
=
"JDBC.Password"
value
=
"${password}"
/>
</
dataSource
>
</
transactionManager
>
<!-- 这里可以写多个实体的映射文件 -->
<
sqlMap
resource
=
"com/ligang/Student.xml"
/>
</
sqlMapConfig
>
|
4、Student.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?
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使得我们在下面使用Student实体类的时候不需要写包名 -->
<
typeAlias
alias
=
"Student"
type
=
"com.ligang.Student"
/>
<!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
<
select
id
=
"findAll"
resultClass
=
"Student"
>
select * from student
</
select
>
<!-- parameterClass表示参数的内容 -->
<
select
id
=
"findByID"
parameterClass
=
"String"
resultClass
=
"Student"
>
select * from student where id = #id#
</
select
>
<
insert
id
=
"insertStudent"
parameterClass
=
"Student"
>
insert into Student(id,name,age,address) values(#id#,#name#,#age#,#address#)
<!-- 返回自动增长值 -->
<
selectKey
resultClass
=
"String"
keyProperty
=
"id"
>
select @@identity as inserted
</
selectKey
>
</
insert
>
<
delete
id
=
"deleteStudentByID"
parameterClass
=
"String"
>
delete from student where id = #id#
</
delete
>
<
delete
id
=
"deleteStudent"
parameterClass
=
"Student"
>
delete from Student where id = #id#
</
delete
>
<
update
id
=
"updateStudent"
parameterClass
=
"Student"
>
update student set name=#name#,age=#age#,address=#address# where id = #id#
</
update
>
<!-- 模糊查询,使用$代替#。此种方法就是去掉了类型检查,使用字符串连接,不过可能会有sql注入风险-->
<
select
id
=
"selectByLike"
parameterClass
=
"String"
resultClass
=
"Student"
>
select * from student where name like '%$name$%'
</
select
>
<!-- 多条件组合查询 -->
<!-- 方法一(对象构造查询参数) -->
<!-- 项目中在写ibatis中的sql语句时,where user_id in (#user_id_list# ),运行时总是不行,这里不该用#,而应该用$,区别如下:
1.#是把传入的数据当作字符串,如#user_id_list#传入的是1,2,则sql语句生成是这样,in ('1,2') ,当然不可以
2.$传入的数据直接生成在sql里,如#user_id_list#传入的是1,2,则sql语句生成是这样,in(1,2) 这就对了.
3.#方式能够很大程度防止sql注入.
4.$方式无法方式sql注入.
5.$方式一般用于传入数据库对象.例如传入表名.
6.一般能用#的就别用$.
直观的说
#str# 出来的效果是 'str'
$str$ 出来的效果是 str
另外 ##只能用在特定的几个地方 $$可以用在任何地方 比如 order by $str$
你甚至可以直接写 $str$ 把 order by 这个字串放在str里传进来 -->
<
select
id
=
"findByCon1"
parameterClass
=
"Student"
resultClass
=
"Student"
>
select * from student where name like '%$name$%' and age >= #age#
</
select
>
<!-- 方法二(map封装查询参数) -->
<
parameterMap
class
=
"java.util.HashMap"
id
=
"paramMap"
>
<
parameter
property
=
"name"
/>
<
parameter
property
=
"age"
/>
</
parameterMap
>
<
select
id
=
"findByCon2"
parameterMap
=
"paramMap"
resultClass
=
"Student"
>
select * from student where name like ? and age >= ?
</
select
>
</
sqlMap
>
|
5、JAVA代码 。
实体类:略 。
Dao:略 。
DaoImpl:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package
com.ligang;
import
java.io.IOException;
import
java.io.Reader;
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
com.ibatis.common.resources.Resources;
import
com.ibatis.sqlmap.client.SqlMapClient;
import
com.ibatis.sqlmap.client.SqlMapClientBuilder;
public
class
StudentDaoImpl
implements
StudentDao {
public
static
SqlMapClient sqlMapClient =
null
;
static
{
try
{
Reader reader = Resources.getResourceAsReader(
"com/ligang/SqlMapConfig.xml"
);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
}
catch
(IOException e) {
e.printStackTrace();
}
}
public
List<Student> findAll() {
List<Student> list =
null
;
try
{
list = sqlMapClient.queryForList(
"findAll"
);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
list;
}
public
Student findByID(String id){
Student student =
null
;
try
{
student = (Student) sqlMapClient.queryForObject(
"findByID"
, id);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
student;
}
public
void
addStudent(Student student){
try
{
sqlMapClient.insert(
"insertStudent"
,student);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
public
void
deleteStudentByID(String id){
try
{
sqlMapClient.delete(
"deleteStudentByID"
,id);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
public
void
deleteStudent(Student student){
try
{
sqlMapClient.delete(
"deleteStudent"
,student);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
public
void
updateStudent(Student student){
try
{
sqlMapClient.update(
"updateStudent"
, student);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
public
List<Student> findByCon(String name){
List<Student> stuList =
new
ArrayList<Student>();
try
{
stuList = sqlMapClient.queryForList(
"selectByLike"
,name);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
stuList;
}
public
List<Student> findByCon(Student student){
List<Student> stuList =
new
ArrayList<Student>();
try
{
stuList = sqlMapClient.queryForList(
"findByCon1"
,student);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
stuList;
}
public
List<Student> findByCon(Map map){
List<Student> stuList =
new
ArrayList<Student>();
try
{
stuList = sqlMapClient.queryForList(
"findByCon2"
,map);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
stuList;
}
}
|
总结 。
通过学习我们会发现,Hibernate体系中的内容真的很多,而ibatis更容易上手,小巧灵活。本文有关ibatis搭建Java项目的介绍就到这里,希望对大家有所帮助.
原文链接:http://blog.csdn.net/ligang2585116/article/details/43410697 。
最后此篇关于ibatis学习之搭建Java项目的文章就讲到这里了,如果你想了解更多关于ibatis学习之搭建Java项目的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
为了构建 CentOS 6.5 OSM 切片服务器,我正在寻找一些文档和/或教程。 我试过this one正如我在我的 previous post 中所说的那样但它适用于 Ubuntu 14.04,而
我正在寻找可用于集成任何源代码控制管理系统的通用 git 桥(如 git-svn、git-p4、git-tfs)模板。 如果没有这样的模板,至少有一些关于如何在 git 端集成基本操作的说明(对于其他
1、前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群。 redis有两种高可用的方案: High availabilit
简介 前提条件: 确保本机已经安装 VS Code。 确保本机已安装 SSH client, 并且确保远程主机已安装 SSH server。 VSCode 已经安装了插件 C/
为什么要用ELK ELK实际上是三个工具,Elastricsearch + Logstash + Kibana,通过ELK,用来收集日志还有进行日志分析,最后通过可视化UI进行展示。一开始业务量比
在日常办公当中,经常会需要一个共享文件夹来存放一些大家共享的资料,为了保证文件数据的安全,最佳的方式是公司内部服务器搭建FTP服务器,然后分配多个用户给相应的人员。今天给大家分享FileZilla搭
最近由于业务需要,开始进行 Flutter 的研究,由于 Flutter 的环境搭建在官网上有些细节不是很清楚,笔者重新整理输出 1. 配置镜像 由于在国内访问 Flutter
目录 1. 安装go软件包 2. 配置系统变量 3. 安装git 4. 设置go代理 5. 下载gin框架 6. 创建项目 7.
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任
上篇文章给大家介绍了使用docker compose安装FastDfs文件服务器的实例详解 今天给大家介绍如何使用 docker compose 搭建 fastDFS文件服务器,内容详情如下所示:
目录 1.创建Maven 2.Maven目录和porm.xml配置 3.配置Tomcat服务器 1.创建Maven
laravel 官方提供 homestead 和 valet 作为本地开发环境,homestead 是一个官方预封装的 vagrant box,也就是一个虚拟机,但是跟 docker 比,它占用体积
这个tutorial显示了 Razor Pages 在 Asp.Net Core 2 中的实现。但是,当我运行 CLI 命令时: dotnet aspnet-codegenerator razorp
我创建了一个单独的类库项目来存储数据库上下文和模型类。在同一解决方案中,我创建了一个 ASP.NET MVC 项目并引用了类库项目,并在项目的 Web.config 文件中包含了数据库上下文的连接字符
关于代码托管,公司是基于Gitlab自建的,它功能全而强大,但是也比较重,我个人偏向于开源、小巧、轻便、实用,所以就排除了Github,在Gogs和Gitea中选者。Gogs在Github有38
目录 1、高可用简介 1.1 高可用整体架构 1.2 基于 QJM 的共享存储系统的数据同步机制分析 1.3 NameNode 主
Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。 在高并发连接的情况
对于我们的 ASP.NET Core 项目,我们使用包管理器控制台中的 Scaffold-DbContext 搭建现有数据库。 每次我们做脚手架时,上下文类与所有实体一起生成,它包含调用 option
我正在使用 .net 核心 2.0。我已经安装了以下 nuget 包:1: Microsoft.AspNetCore.All2: Microsoft.EntityFrameworkCore.Tools
我正在使用 NetBeans 及其 RAD 开发功能开发 JEE6 JSF 应用程序。我想使用脚手架来节省更新 Controller 和模型 View 的时间。 OneToMany 关联在 View
我是一名优秀的程序员,十分优秀!