Let's set the environment of Spring + Mybatis + Oracle in Intellij

1. Create Project.

1
File > New > Project

Please select Gradle Category and then You can see many various libraries for this. then Let’s start to add 2 options named Java , Web like below. If you completed then please click Next button.

You can type any sentences or words for GroupId, ArtifactId If you want. But If you do, maybe you will confuse because of the java package, so I recommand to follow like my styles.

1
2
GroupId : com.kidongyun
ArtifactId : SpringMybatisOracle

If you completed, Let’s press Next button.

Project Name is maybe same with the ArtifactId, It’s not considerable to change the name. so If you want, You can change the name. But If you are a novice about the Spring Framework, then Let’s follow me for preventing any errors.

1
Project name : SpringMybatisOracle

Press Finish button.

then you can see below pictures. that is the final screen enable seen if you success above process perfectly.

2. Configure the build.gradle

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

plugins {
id 'java'
id 'war'
}

apply plugin: 'war'

group 'com.kidongyun'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
runtime 'javax.servlet:jstl:1.1.2'
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'

compile 'org.springframework:spring-webmvc:4.3.18.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '3.1.0.RELEASE'

compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.1.0'
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
}

Please copy & paste the above code to your project. Actually mavenCentral() is default repository offered by Maven. But there can’t serve OJDBC library directly cause Copyright of Oracle. So We should include the code maven() { url …} to use ojdbc

1
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'

if you want to use @Resource annotation, Let’s add the above dependency.

1
2
compile 'org.springframework:spring-webmvc:4.3.18.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '3.1.0.RELEASE'

Those are the things for applying basic functions of Spring Framework

1
compile group: 'com.oracle', name: 'ojdbc6', version: '12.1.0.1-atlassian-hosted'

If you don’t add the new repository, then your project maybe will have errors what can’t get ojdbc6 library at above code.

1
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'

For Connection Pool. Your DB System is better than not when use this.

3. Import the local ‘ojdbc6.jar’ file

Let’s download ojdbc6.jar

You have to read this thing not the above. the repository for ‘OJDBC’ is not working after yesterday. for solving this problem, We will import the ‘OJDBC’ library locally. that is we will download the ‘.jar’ file related to ‘OJDBC’ and import that thing like the below

Create new folder named ‘lib’ for the local jar file ‘OJDBC’.

1
2
3

workspace/src/main/webapp/WEB-INF> mkdir lib

and then Let’s copy and paste your local file.

Press Ctrl + Shift + Alt + S to open the Project Structure window and

src
1
Ctrl + Shift + Alt + S -> Libraries Tab -> '+' Button -> Java > Select your file > Apply 

4. Delete index.jsp files for new one

1
> rm src/main/webapp/index.jsp

5. Make the Directories and Files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> mkdir src/main/webapp/WEB-INF
> touch src/main/webapp/WEB-INF/web.xml
> touch src/main/webapp/WEB-INF/dispatcher-servlet.xml
> touch src/main/webapp/WEB-INF/applicationContext.xml

> mkdir src/main/webapp/WEB-INF/view
> touch src/main/webapp/WEB-INF/view/index.jsp

> touch src/main/resources/mybatis-config.xml
> mkdir src/main/resources/mappers
> touch src/main/resources/mappers/personMappers.xml

> mkdir src/main/java/com/kidongyun
> mkdir src/main/java/com/kidongyun/vo/
> touch src/main/java/com/kidongyun/vo/PersonVO.java
> mkdir src/main/java/com/kidongyun/dao
> touch src/main/java/com/kidongyun/dao/PersonDAO.java /* Interface */
> mkdir src/main/java/com/kidongyun/service
> touch src/main/java/com/kidongyun/service/PersonService.java
> mkdir src/main/java/com/kidongyun/controller
> touch src/main/java/com/kidongyun/controller/PersonController.java

PersonDAO.java is java interface file. so Don’t make this as just class type file

6. Setiing for each files.

src/main/webapp/WEB-INF/web.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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

src/main/webapp/WEB-INF/dispatcher-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.kidongyun" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

src/main/webapp/WEB-INF/applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property value="oracle.jdbc.driver.OracleDriver" name="driverClassName" />
<property value="jdbc:oracle:thin:@localhost:1521:XE" name="url" />
<property value="c##scott" name="username" />
<property value="tiger" name="password" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mappers/**/*.xml"></property>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kidongyun" />
</bean>
</beans>

src/main/webapp/WEB-INF/view/index.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
${data}
</body>
</html>

src/main/resources/mybatis-config.xml

1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0/EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

src/main/resources/mappers/personMappers.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kidongyun.dao.PersonDAO">

<select id="selectPerson" resultType="com.kidongyun.vo.PersonVO">
SELECT * FROM PERSON
</select>

</mapper>

src/main/java/com/kidongyun/vo/PersonVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kidongyun.vo;

public class PersonVO {
String name;
int age;
String birthday;

// ALT + INSERT -> GENERATE toString()
@Override
public String toString() {
return "PersonVO{" +
"name='" + name + '\'' +
", age=" + age +
", birthday='" + birthday + '\'' +
'}';
}
}

src/main/java/com/kidongyun/dao/PersonDAO.java

1
2
3
4
5
6
7
8
9
10
11
package com.kidongyun.dao;

import com.kidongyun.vo.PersonVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PersonDAO {
public List<PersonVO> selectPerson();
}

src/main/java/com/kidongyun/service/PersonService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.kidongyun.service;

import com.kidongyun.dao.PersonDAO;
import com.kidongyun.vo.PersonVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PersonService {

@Resource
private PersonDAO personDAO;

public List<PersonVO> selectPerson() {
return personDAO.selectPerson();
}
}

src/main/java/com/kidongyun/controller/PersonController.java

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
package com.kidongyun.controller;

import com.kidongyun.service.PersonService;
import com.kidongyun.vo.PersonVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class PersonController {

@Resource
PersonService personService;

@RequestMapping(value = "/")
public String hello(Model model){
List<PersonVO> data = personService.selectPerson();

model.addAttribute("data", data.toString());
return "index";
}
}

7. Make the Person Table using Oracle DB

1
2
3
4
5
CREATE TABLE PERSON (
NAME VARCHAR2(10),
AGE NUMBER(3),
BIRTHDAY DATE
)

It’s for making the new PERSON Table at Oracle.

1
INSERT INTO PERSON VALUES ('martin', 18, SYSDATE);

and this query for putting the dummy datas.

Share