To build the Spring + Mybatis + Oracle using Java configuration without XML

1. The need of Spring Framework -> Third party place for creating the java objects.

Have you ever thought about the reason why the Spring framework was borned? Actually When I studied this framework, I can’t understand the need of this. Everything can be implemented without the ‘Spring Framework’ but I realized the important need of ‘Spring’ at this time, Um… Have you ever felt the difficult to create the instance of the Java Object? When you have 3 Java objects, We should create these Java object using ‘new’ keyword at the somewhere. But These object are really independent then It’s really hard to determine the place where is better than another. Most of developers use the third party place like the ‘main function’ for create these java objects. Actually The important thing of the Spring Framework can offer the third party place for creating Java Objects and We say this place as ‘Spring Container’ and The java objects using the ‘Spring Container’ are named ‘Bean’.

2. Create the new project

Plase follow the below steps. First of all We should make a new project for learning the process of building the Spring + Mybatis + Oracle using Java configuration.

1
File > New > Project

then You can see the below picture, Please click the ‘Web’ option at this window. totally You should get two options. If you finished then go to next window, Press the ‘Next’ button.

1
2
3
4
5
6
7

Selected Options.

- Java

- Web

You should write GroupId, ArtifactId. please just typing like me. It’s not important at this topic. press ‘Next’ button.

1
2
3
GroupId : com.kidongyun

ArtifactId : SpringMybatisOracleJavaConfiguration

If you type correctly at the above step, then your project name is correct either like the below. press ‘Next’ button.

1
2
3
Project name : SpringMybatisOracleJavaConfiguration

Project location : The place you want

Press ‘Finish’ button.

3. Configure the ‘build.gradle’

After creating the new project, We gotta get the dependencies related to the Spring, Mybatis, Oracle…

Please copy and paste the code to your ‘build.gradle’

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
32

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.3.2'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.6'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.0'
}

You should type the version of each things. If you don’t, You may encounter the error at the console especially mybatis and mybatis-spring. and the detail role of each dependency is written at here.

4. Import the local ‘ojdb6.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 ‘ojdbc6.jar’.

1
2
3
workspace/src/main/webapp> mkdir WEB-INF

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

and then Let’s copy and paste ‘ojdbc6.jar’ into the ‘lib’ folder.

And follow the below.

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

5. Delete index.jsp files for new one

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

6. Make the Directories and Files.

For setting this proejct, You should a lot of the directories, and files. You should keep the concentration when you make these.

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

workspace> mkdir src/main/java/com/kidongyun

workspace/src/main/java/com/kidongyun> mkdir config
workspace/src/main/java/com/kidongyun/config> touch WebConfig.java
workspace/src/main/java/com/kidongyun/config> touch ServletConfig.java
workspace/src/main/java/com/kidongyun/config> touch DatabaseConfig.java

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

workspace/src/main/java/com/kidongyun> mkdir vo
workspace/src/main/java/com/kidongyun/vo> touch PersonVO.java

workspace/src/main/java/com/kidongyun> mkdir dao
workspace/src/main/java/com/kidongyun/dao> touch PersonDAO.java // It's Interface.

workspace/src/main/java/com/kidongyun> mkdir service
workspace/src/main/java/com/kidongyun/service> touch PersonService.java

workspace/src/main/java/com/kidongyun> mkdir controller
workspace/src/main/java/com/kidongyun/controller> touch PersonController.java

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

7. Configure each files.

WebConfig.java

It has really similar function with ‘web.xml’. The main function of this file is actually to create the ‘Root container’ and ‘Servlet’. as you know, ‘Root Container’ is the fundamental third party place of the ‘Bean’.

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

package com.kidongyun.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebConfig implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

/** Create the Root Container. */
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.kidongyun.config");

/** Create the Servlet. */
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}

ServletConfig.java

Actually if you want, you can create ‘Servlet’ more than one. So ‘ServletConfig.java’ file is the setting for each servlet. The core of ‘Servlet’ is to take the request of a client and to return the response of the request from the client. You will configure about these setting in this file. It equal the ‘dispatcher-servlet.xml’ file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

package com.kidongyun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration /** It means the Spring Container.*/
@EnableWebMvc
@ComponentScan("com.kidongyun") /** <context:component-scan base-package="com.kidongyun" /> */
public class ServletConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
}


DatabaseConfig.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
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

package com.kidongyun.config;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.annotation.Resource;

@Configuration
@MapperScan("com.kidongyun.dao")
public class DatabaseConfig {

@Resource
private ApplicationContext applicationContext;

/** Datasource is the object for connecting between database and java from JDBC. */
@Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName("oracle.jdbc.driver.OracleDriver");
source.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
source.setUsername("c##scott");
source.setPassword("tiger");

return source;
}

/** Setting Datasource, Mybatis configuration file and mapper directories */
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml"));
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:mappers/**/*.xml"));

return sqlSessionFactoryBean.getObject();
}

@Bean
public SqlSession sqlSession() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
return sqlSessionTemplate;
}
}

mybatis-config.xml

1
2
3
4
5
6

<?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>

personMappers.xml

1
2
3
4
5
6
7
8
9
10
11
12

<?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>

PersonVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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 + '\'' +
'}';
}
}

PersonDAO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14

/** It's interface */

package com.kidongyun.dao;

import com.kidongyun.vo.PersonVO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

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

PersonService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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();
}
}

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
26
27

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";
}
}

index.jsp

1
2
3
4
5
6
7
8
9
10
11

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
${data}
</body>
</html>

8. 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.

9. Configure the tomcat for executing the web server from local.

Skip.

Additional. Change the ‘WebConfig.java’ for adding the ‘RootConfig’

If you follow the above code style, You can’t configure the ‘Root Context’ file separately. But if you follow this code style then you can do that. First of all, You should make the ‘RootConfig.java’ file into the com/kidongyun/config/ directory.

RootConfig.java

1
2
3
4
5
6
7
8
9
10
11

package com.kidongyun.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.kidongyun"})
public class RootConfig {
}

When you want to create the some beans, You can do over there. and You have to change your ‘WebConfig.java’ file for recognizing about your new ‘RootConfig.java’ file.

WebConfig.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

package com.kidongyun.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { ServletConfig.class };
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

}

‘getRootConfigClasses()’ method is for setting the ‘RootConfig.java’ and ‘getServletConfigClasses()’ method is for the ‘ServletConfig.java’ I think it’s more better idea than the above configuration.

Share