ss和ibatis框架,连接oracle的配置文件该怎么配
展开全部
1.下载ibatis Jar包,我用的是ibatis-2.3.0.677.jar
2.添加iBatis的config配置文件
com.lubby.bean.SqlMapConfig.xml
[html] view plain copy
<?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/lubby/bean/SqlMap.properties" /> -->
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC">
<!-- 数据源 -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="org.postgresql.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:postgresql://127.0.0.1:5432/lubby" />
<property name="JDBC.Username" value="admin" />
<property name="JDBC.Password" value="admin" />
</dataSource>
</transactionManager>
<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="com/lubby/bean/Student.xml" />
</sqlMapConfig>
3.添加实体类
com.lubby.bean.Student.java
[html] view plain copy
public class Student {
private String sid;
private String name;
private int age;
public Student() {
super();
}
public Student(String sid, String name, int age) {
super();
this.sid = sid;
this.name = name;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", name=" + name + ", age=" + age + "]";
}
}
4.添加实体类的Maping文件
com.lubby.bean.Student.xml
[html] view plain copy
<?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.lubby.bean.Student" />
<!-- 这样以后改了sql,就不需要去改java代码了 -->
<!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
<select id="getAllStudent" resultClass="Student">
select * from student
</select>
<!-- parameterClass表示参数的内容 -->
<!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
<select id="getStudentById" parameterClass="String" resultClass="Student">
select * from student where sid=#id#
</select>
</sqlMap>
5.DAO以及实现DAO
com.lubby.dao.StudentDAO.java
[java] view plain copy
public interface StudentDAO {
/**
* 获取所有学生
* @return 所有学生信息列表
*/
public List<Student> getAllStudent();
/**
* 根据学生ID查询信息
* @return 所查学生信息
*/
public Student getStudentById(String id);
}
com.lubby.daoimpl.StudentDAOImpl.java
[java] view plain copy
package com.lubby.daoimpl;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.lubby.bean.Student;
import com.lubby.dao.StudentDAO;
public class StudentDAOImpl implements StudentDAO {
private static SqlMapClient sqlMapClient = null;
// 读取配置文件
static {
try {
Reader reader = Resources.getResourceAsReader("com/lubby/bean/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<Student> getAllStudent() {
List<Student> list = null;
try {
list = sqlMapClient.queryForList("getAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Student getStudentById(String id) {
Student student = null;
try {
student = (Student) sqlMapClient.queryForObject("getStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
}
6.调用DAO查询
com.lubby.test.IbatisTest.java
[java] view plain copy
public class IbatisTest {
public static void main(String[] args) {
StudentDAO stuDaoImpl = new StudentDAOImpl();
List<Student> list = new ArrayList<>();
list = stuDaoImpl.getAllStudent();
for (Student stu : list) {
System.out.println(stu);
}
Student student = stuDaoImpl.getStudentById("003");
System.out.println("ID为004的学生信息:" + student);
}
}
7.结果显示
[plain] view plain copy
Student [sid=001, name=Tom, age=22]
Student [sid=002, name=Jack, age=23]
Student [sid=003, name=Tom, age=20]
Student [sid=004, name=Alice, age=18]
ID为004的学生信息:Student [sid=003, name=Tom, age=20]
2.添加iBatis的config配置文件
com.lubby.bean.SqlMapConfig.xml
[html] view plain copy
<?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/lubby/bean/SqlMap.properties" /> -->
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC">
<!-- 数据源 -->
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="org.postgresql.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:postgresql://127.0.0.1:5432/lubby" />
<property name="JDBC.Username" value="admin" />
<property name="JDBC.Password" value="admin" />
</dataSource>
</transactionManager>
<!-- 这里可以写多个实体的映射文件 -->
<sqlMap resource="com/lubby/bean/Student.xml" />
</sqlMapConfig>
3.添加实体类
com.lubby.bean.Student.java
[html] view plain copy
public class Student {
private String sid;
private String name;
private int age;
public Student() {
super();
}
public Student(String sid, String name, int age) {
super();
this.sid = sid;
this.name = name;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", name=" + name + ", age=" + age + "]";
}
}
4.添加实体类的Maping文件
com.lubby.bean.Student.xml
[html] view plain copy
<?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.lubby.bean.Student" />
<!-- 这样以后改了sql,就不需要去改java代码了 -->
<!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->
<select id="getAllStudent" resultClass="Student">
select * from student
</select>
<!-- parameterClass表示参数的内容 -->
<!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->
<select id="getStudentById" parameterClass="String" resultClass="Student">
select * from student where sid=#id#
</select>
</sqlMap>
5.DAO以及实现DAO
com.lubby.dao.StudentDAO.java
[java] view plain copy
public interface StudentDAO {
/**
* 获取所有学生
* @return 所有学生信息列表
*/
public List<Student> getAllStudent();
/**
* 根据学生ID查询信息
* @return 所查学生信息
*/
public Student getStudentById(String id);
}
com.lubby.daoimpl.StudentDAOImpl.java
[java] view plain copy
package com.lubby.daoimpl;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.lubby.bean.Student;
import com.lubby.dao.StudentDAO;
public class StudentDAOImpl implements StudentDAO {
private static SqlMapClient sqlMapClient = null;
// 读取配置文件
static {
try {
Reader reader = Resources.getResourceAsReader("com/lubby/bean/SqlMapConfig.xml");
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<Student> getAllStudent() {
List<Student> list = null;
try {
list = sqlMapClient.queryForList("getAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Student getStudentById(String id) {
Student student = null;
try {
student = (Student) sqlMapClient.queryForObject("getStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
}
6.调用DAO查询
com.lubby.test.IbatisTest.java
[java] view plain copy
public class IbatisTest {
public static void main(String[] args) {
StudentDAO stuDaoImpl = new StudentDAOImpl();
List<Student> list = new ArrayList<>();
list = stuDaoImpl.getAllStudent();
for (Student stu : list) {
System.out.println(stu);
}
Student student = stuDaoImpl.getStudentById("003");
System.out.println("ID为004的学生信息:" + student);
}
}
7.结果显示
[plain] view plain copy
Student [sid=001, name=Tom, age=22]
Student [sid=002, name=Jack, age=23]
Student [sid=003, name=Tom, age=20]
Student [sid=004, name=Alice, age=18]
ID为004的学生信息:Student [sid=003, name=Tom, age=20]
快又稳
2024-10-28 广告
2024-10-28 广告
在Linux环境下配置基于域名的虚拟主机,需安装Apache或Nginx等Web服务器,并编辑配置文件。以Apache为例,需创建虚拟主机配置文件,指定域名、文档根目录等,然后启用该配置文件并重启Apache服务。同样,Nginx也需在相应...
点击进入详情页
本回答由快又稳提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询