^_^
。温馨提醒:不同的人部署,部分步骤可能会有些不同,但大体流程都是一样的,咱刚开始学,只要能成功部署一次,体验常用的
增,删,修,查
即可。后面用多了,自然就熟悉了。
person
(先插入一条记录,测试用)SQL> desc person;
Name Type Nullable Default Comments
--------- ------------ -------- ------- --------
PERSON_NO NUMBER(5) Y
NAME VARCHAR2(30) Y
SEX VARCHAR2(2) Y
BIRTHDAY DATE Y
CREATE OR REPLACE PROCEDURE SCOTT.pro_get_salsumbyename(v_name IN scott.emp.ename%TYPE,
v_sal OUT NUMBER) AS
BEGIN
SELECT SUM(t.sal) INTO v_sal FROM scott.emp t WHERE t.ename = v_name;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END pro_get_salsumbyename;
新建
一个即可。ojdbc6.jar
(pom.xml 文件下
)db.properties(resources 文件下)
:配置你本地 Oracle 用户名,密码,url手动安装
.ojdbc6.jar
(参考: D:\app\YoYo\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
)mvn install:install-file -Dfile=D:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -DgeneratePom=true
Import changes
找不到 XXX 配置文件
,多半是这里没配置好
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.company.mybaitsdemogroupId>
<artifactId>mybaitsdemoartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc6artifactId>
<version>11.2.0.1.0version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>compilescope>
dependency>
dependencies>
<build>
<finalName>MybaitsDemofinalName>
<resources>
<resource>
<directory>${basedir}/src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>${basedir}/src/main/resourcesdirectory>
resource>
resources>
build>
project>
项目启动时,
第一个
进入的配置文件
resources
文件夹下新建 mybaits-config.xml
文件头文件
(如果爆红,尝试导入包快捷键 Alter Enter
)
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<typeAlias type="java.lang.Integer" alias="Integer"/>
typeAliases>
<environments default="dev_Oracle">
<environment id="dev_Oracle">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="com.company.mapper/PersonMapper.xml"/>
mappers>
configuration>
db.properties
log4j.properties
同理jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=SYSTEM
jdbc.password=Wy1239221426
<mapper namespace="com.company.mapper.PersonMapper">
<resultMap id="personMap" type="com.company.domain.PersonDomain">
<result column="PERSON_NO" property="personNo" jdbcType="INTEGER"/>
<result column="NAME" property="name" jdbcType="VARCHAR"/>
<result column="SEX" property="sex" jdbcType="VARCHAR"/>
<result column="BIRTHDAY" property="birthday" jdbcType="DATE"/>
resultMap>
<select id="select" resultMap="personMap">
select * from person
select>
<select id="selectByPersonNo" parameterType="Integer" resultMap="personMap">
select * from person where person_no = #{personNo}
select>
<insert id="insert">
insert into person(PERSON_NO, NAME, SEX, BIRTHDAY)
values(#{personNo}, #{name}, #{sex}, #{birthday})
insert>
<update id="update" parameterType="com.company.domain.PersonDomain">
update person set name = #{name}, sex = #{sex} where person_no = #{personNo}
update>
<delete id="delete" parameterType="Integer">
delete from person where person_no = #{personNo}
delete>
<parameterMap id="SalSumByEname" type="java.util.Map">
<parameter property="v_name" mode="IN" jdbcType="VARCHAR"/>
<parameter property="v_sal" mode="OUT" jdbcType="INTEGER"/>
parameterMap>
<select id="getSalSumByEname" parameterMap="SalSumByEname" statementType="CALLABLE">
{CALL scott.pro_get_salsumbyename(?, ?)}
select>
mapper>
package com.company.domain;
import java.util.Date;
/**
* @Description: 实体类 Person
* @Author: YoYo
* @Date: 2019-12-18 22:55
*/
public class PersonDomain {
private int personNo;
private String name;
private String sex;
private Date birthday;
public int getPersonNo() {
return personNo;
}
public void setPersonNo(int personNo) {
this.personNo = personNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "PersonDomain{" +
"personNo=" + personNo +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
package com.company.controller;
import com.company.domain.PersonDomain;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class DemoTest {
private Logger logger = Logger.getLogger(DemoTest.class.toString());
private SqlSessionFactory sqlsessionFactory;
@Before
public void init() {
try {
String resource = "mybaits-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
sqlsessionFactory = new SqlSessionFactoryBuilder().build(is);
is.close();
logger.info("主配置文件读取成功: mybaits-config.xml");
} catch (IOException e) {
logger.info("初始化配置文件报错:mybaits-config.xml");
e.printStackTrace();
}
}
@Test
public void selectByPK() {
SqlSession sqlSession = sqlsessionFactory.openSession();
PersonDomain personDomain = new PersonDomain();
personDomain.setPersonNo(1);
List<PersonDomain> selectOne = sqlSession.selectList("selectByPersonNo", personDomain);
logger.info("查询一条记录:开始");
System.out.println(selectOne);
logger.info("查询一条记录:结束");
sqlSession.close();
}
@Test
public void select() {
SqlSession sqlSession = sqlsessionFactory.openSession();
List<PersonDomain> selectAll = sqlSession.selectList("select");
logger.info("查询所有记录:开始");
for (PersonDomain p : selectAll
) {
System.out.println(p);
}
logger.info("查询所有记录:结束");
sqlSession.close();
}
@Test
public void insert(){
SqlSession sqlSession = sqlsessionFactory.openSession();
PersonDomain personDomain = new PersonDomain();
personDomain.setPersonNo(2);
personDomain.setName("小悠悠");
personDomain.setSex("女");
personDomain.setBirthday(new Date());
int insertPersonByPersonNo = sqlSession.insert("insert", personDomain);
System.out.println("插入了 " + insertPersonByPersonNo + " 条记录");
sqlSession.commit();
sqlSession.close();
}
@Test
public void update(){
SqlSession sqlSession = sqlsessionFactory.openSession();
PersonDomain personDomain = new PersonDomain();
personDomain.setPersonNo(1);
personDomain.setName("小游子");
personDomain.setSex("女");
int updatePersonByPersonNo = sqlSession.update("update", personDomain);
System.out.println("更新了 " + updatePersonByPersonNo + " 条记录");
sqlSession.commit();
sqlSession.close();
}
@Test
public void delete(){
SqlSession sqlSession = sqlsessionFactory.openSession();
PersonDomain personDomain = new PersonDomain();
personDomain.setPersonNo(2);
int deletePersonByPersonNo = sqlSession.delete("delete", personDomain);
System.out.println("删除了 " + deletePersonByPersonNo + " 条记录");
sqlSession.commit();
sqlSession.close();
}
@Test
public void procedure(){
SqlSession sqlSession = sqlsessionFactory.openSession();
Map<String, Object> map = new HashMap<String, Object>();
map.put("v_name", "SMITH");
sqlSession.selectOne("getSalSumByEname", map);
System.out.println(map.get("v_sal"));
sqlSession.close();
}
}