微信公众平台 java开发 能用oracle数据库吗
2016-04-05 · 百度知道合伙人官方认证企业
微信公众平台是可以开发java调用oracle这类型的接口的,一般通过MyBatis连接Oracle数据库。
举例如下:
1、先建立一个数据库表,名为PERSON_INFO,建表SQL如下:
create TABLE PERSON_INFO
(
id number(12,0) PRIMARY KEY,
name varchar2(20) NOT NULL,
gender char(1) DEFAULT ' ',
remark varchar2(1000),
input_date number(10,0) DEFAULT to_number(to_char(sysdate,'yyyymmdd')),
input_time number(10,0) DEFAULT to_number(to_char(sysdate,'hh24miss'))
);
2、编写java程序,项目中文件的上下级关系如图:
3、构建mybatis配置映射关系
1)、导入JAR包:mybatis-3.2.2.jar、ojdbc14-10.2.0.2.0.jar
2)、建立MyBatis配置文件mybatis-config.xml
4、建立xml文件:PersonInfoMapper.xml,其中实现了一个SQL语句:selectAllPersonInfo,查询所有的PERSON_INFO信息
5、建立对应的Java类:PersonInfo,其中各属性对应于数据表PERSON_INFO中的各字段
public class PersonInfo {
Long id;
String name;
String gender;
String remark;
Long inputDate;
Long inputTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Long getInputDate() {
return inputDate;
}
public void setInputDate(Long inputDate) {
this.inputDate = inputDate;
}
public Long getInputTime() {
return inputTime;
}
public void setInputTime(Long inputTime) {
this.inputTime = inputTime;
}
}
6、建立对应的Java类:PersonInfoMapper
import java.util.List;
public interface PersonInfoMapper {
List<PersonInfo> selectAllPersonInfo();
7、建立一个类MyBatisTest用于存放main函数,查询PERSON_INFO表中所有的数据并打印
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* MyBatis使用测试
* @author pieryon
* @date 2016年4月1日
* @time 下午21:47:01
* @remark
*
*/
public class MyBatisTest {
public static void main(String[] args) {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
PersonInfoMapper mapper = session.getMapper(PersonInfoMapper.class);
List<PersonInfo> personInfos = mapper.selectAllPersonInfo();
if (personInfos == null) {
System.out.println("The result is null.");
} else {
for (PersonInfo personInfo : personInfos) {
System.out.println("---PersonInfo---");
System.out.println("name:" + personInfo.name);
System.out.println("gender:" + personInfo.gender);
System.out.println("remark:" + personInfo.remark);
System.out.println("inputDate:" + personInfo.inputDate);
System.out.println("inputTime:" + personInfo.inputTime);
System.out.println();
}
}
} finally {
session.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
8、运行main函数,控制台输出结果如下:
---PersonInfo---
name:Tsybius
gender:m
remark:-
inputDate:20160229
inputTime:225703
---PersonInfo---
name:Galatea
gender:f
remark:-
inputDate:20160228
inputTime:123456