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’
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.
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.
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’.
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.
/** Datasource is the object for connecting between database and java from JDBC. */ @Bean public DriverManagerDataSource dataSource() { DriverManagerDataSourcesource=newDriverManagerDataSource(); 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 { SqlSessionFactoryBeansqlSessionFactoryBean=newSqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml")); sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath:mappers/**/*.xml"));
CREATETABLE PERSON ( NAME VARCHAR2(10), AGE NUMBER(3), BIRTHDAY DATE )
It’s for making the new PERSON Table at Oracle.
1
INSERTINTO 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.
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.
‘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.