Skip to content

Commit d6cb0b9

Browse files
author
javaage
committed
solve mybatis problem
1 parent 3369b56 commit d6cb0b9

File tree

5 files changed

+174
-26
lines changed

5 files changed

+174
-26
lines changed

flexible/postgres/pom.xml

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!-- Copyright 2017 Google Inc. Licensed under the Apache License, Version
2-
2.0 (the "License"); you may not use this file except in compliance with
3-
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4-
Unless required by applicable law or agreed to in writing, software distributed
5-
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
6-
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
1+
<!-- Copyright 2017 Google Inc. Licensed under the Apache License, Version
2+
2.0 (the "License"); you may not use this file except in compliance with
3+
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
Unless required by applicable law or agreed to in writing, software distributed
5+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
6+
OR CONDITIONS OF ANY KIND, either express or implied. See the License for
77
the specific language governing permissions and limitations under the License. -->
88
<project>
99
<modelVersion>4.0.0</modelVersion>
@@ -21,7 +21,7 @@
2121

2222
<!-- [START properties] -->
2323
<properties>
24-
<!-- INSTANCE_CONNECTION_NAME from Cloud Console > SQL > Instance Details
24+
<!-- INSTANCE_CONNECTION_NAME from Cloud Console > SQL > Instance Details
2525
> Properties or `gcloud sql instances describe <instance> | grep connectionName` -->
2626
<INSTANCE_CONNECTION_NAME>newagent-25039:asia-northeast1:la</INSTANCE_CONNECTION_NAME>
2727
<user>postgres</user>
@@ -36,7 +36,7 @@
3636

3737
<jetty>9.4.4.v20170414</jetty>
3838
<!-- [END_EXCLUDE] -->
39-
<!--
39+
<!--
4040
<sqlURL>jdbc:postgresql://google/${database}?useSSL=false&amp;socketFactoryArg=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.postgres.SocketFactory&amp;user=${user}&amp;password=${password}</sqlURL>
4141
-->
4242
</properties>
@@ -83,16 +83,31 @@
8383
<artifactId>mybatis-generator-core</artifactId>
8484
<version>1.3.5</version>
8585
</dependency>
86-
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-sqlmap -->
86+
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-sqlmap
8787
<dependency>
8888
<groupId>org.apache.ibatis</groupId>
8989
<artifactId>ibatis-sqlmap</artifactId>
9090
<version>3.0-beta-10</version>
9191
</dependency>
92-
93-
</dependencies>
92+
-->
93+
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
94+
<dependency>
95+
<groupId>org.mybatis</groupId>
96+
<artifactId>mybatis</artifactId>
97+
<version>3.4.5</version>
98+
</dependency>
99+
100+
</dependencies>
94101
<build>
95102
<resources>
103+
<resource>
104+
<directory>src/main/java</directory>
105+
<includes>
106+
<include>**/*.properties</include>
107+
<include>**/*.xml</include>
108+
</includes>
109+
<filtering>false</filtering>
110+
</resource>
96111
<resource>
97112
<directory>src/main/resources</directory>
98113
<filtering>true</filtering>

flexible/postgres/src/main/java/com/cisco/la/UserServlet.java

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.cisco.la;
1818

19+
import com.cisco.la.entity.LaUser;
20+
import com.cisco.la.util.MyBatisUtil;
1921
import com.google.common.base.Stopwatch;
2022

2123
import java.io.IOException;
@@ -43,10 +45,12 @@
4345
import javax.servlet.http.HttpServletResponse;
4446

4547
import org.apache.ibatis.io.Resources;
48+
import org.apache.ibatis.session.SqlSession;
4649
import org.apache.ibatis.session.SqlSessionFactory;
4750
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
48-
49-
51+
import com.cisco.la.dao.LaUserDao;
52+
import com.cisco.la.dao.LaRlHistoryDao;
53+
import com.cisco.la.entity.LaRlHistory;
5054

5155
// [START example]
5256
@SuppressWarnings("serial")
@@ -55,23 +59,102 @@ public class UserServlet extends HttpServlet {
5559
Connection conn;
5660

5761
@Override
58-
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
62+
public void doGet(HttpServletRequest req, HttpServletResponse resp){
63+
64+
// try {
65+
//
66+
// Class.forName("org.postgresql.Driver");
67+
// conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/la","postgres","19786028");
68+
// } catch (ClassNotFoundException e) {
69+
// e.getMessage();
70+
// } catch (SQLException e) {
71+
// e.getMessage();
72+
// } finally {
73+
// // Nothing really to do here.
74+
// }
75+
//
76+
// String selectSql = "select hstr_usr_id, hstr_crs_id from la_crs_history";
77+
// try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
78+
// while (rs.next()) {
79+
// String savedIp = rs.getString("hstr_usr_id");
80+
// int test = 0;
81+
// }
82+
// }catch (Exception e){
83+
// e.getMessage();
84+
// }
5985

60-
String url;
6186

62-
Properties properties = new Properties();
6387
try {
64-
properties.load(
65-
getServletContext().getResourceAsStream("/WEB-INF/classes/config.properties"));
66-
url = properties.getProperty("sqlUrl");
67-
} catch (IOException e) {
68-
log("no property", e); // Servlet Init should never fail.
69-
return;
88+
PrintWriter out = resp.getWriter();
89+
resp.setContentType("text/plain");
90+
out.println("connecting to: ");
91+
92+
Reader reader = new InputStreamReader(getServletContext().getResourceAsStream("/WEB-INF/classes/mybatis-config.xml"));
93+
//out.print(reader.read());
94+
//out.println("end");
95+
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
96+
out.println("end1");
97+
SqlSessionFactory sqlSessionFactory = builder.build(reader);
98+
out.println("end2");
99+
SqlSession sqlSession = sqlSessionFactory.openSession();
100+
out.println("end3");
101+
LaRlHistoryDao laRlHistoryDao = sqlSession.getMapper(LaRlHistoryDao.class);
102+
out.println("end4");
103+
LaRlHistory laRlHistory = new LaRlHistory();
104+
laRlHistory.setHstrRlHistory("sys");
105+
laRlHistory.setHstrRlName("sys");
106+
laRlHistory.setHstrUpdateTime(new Date());
107+
laRlHistory.setHstrUsrId("sys");
108+
laRlHistoryDao.insertLaRlHistory(laRlHistory);
109+
out.println("end123");
110+
sqlSession.commit();
111+
} catch (IOException e1) {
112+
try{
113+
PrintWriter out = resp.getWriter();
114+
resp.setContentType("text/plain");
115+
out.println(e1.getMessage());
116+
}catch (IOException e) {
117+
118+
}
70119
}
71120

72-
log("connecting to: " + url);
73-
PrintWriter out = resp.getWriter();
74-
resp.setContentType("text/plain");
75-
out.println("connecting to: " + url);
121+
122+
123+
// try {
124+
// PrintWriter out = resp.getWriter();
125+
// resp.setContentType("text/plain");
126+
// out.println("connecting to: ");
127+
//
128+
// String path = Thread.currentThread().getContextClassLoader().getResource("/mybatis-config.xml").getPath();
129+
// out.println(path);
130+
// Reader reader = Resources.getResourceAsReader(path);
131+
// SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
132+
// SqlSessionFactory sqlSessionFactory = builder.build(reader);
133+
// out.println("end");
134+
// } catch (IOException exp){
135+
// try {
136+
// PrintWriter out = resp.getWriter();
137+
// resp.setContentType("text/plain");
138+
// out.println(exp.getMessage());
139+
// }catch (IOException e){
140+
//
141+
// }
142+
// }
143+
144+
// String path = MyBatisUtil.class.getResource("/mybatis-config.xml").toString();
145+
// System.out.println("path = " + path);
146+
// PrintWriter out = resp.getWriter();
147+
// resp.setContentType("text/plain");
148+
// out.println("connecting to: " + path);
149+
// try {
150+
// MyBatisUtil.getSqlSessionFactory();
151+
// SqlSession sqlSession = MyBatisUtil.getSession();
152+
// LaUserDao laUserDao = sqlSession.getMapper(LaUserDao.class);
153+
// PrintWriter out = resp.getWriter();
154+
// resp.setContentType("text/plain");
155+
// out.println("connecting to: ");
156+
// }catch (Exception exp){
157+
//
158+
// }
76159
}
77160
}

flexible/postgres/src/main/java/resource/generatorConfig.xml renamed to flexible/postgres/src/main/resources/generatorConfig.xml

File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
2+
3+
############################################################
4+
# Handler specific properties.
5+
# Describes specific configuration info for Handlers.
6+
############################################################
7+
8+
org.apache.juli.FileHandler.level = FINE
9+
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
10+
org.apache.juli.FileHandler.prefix = error-debug.
11+
12+
java.util.logging.ConsoleHandler.level = FINE
13+
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
<configuration>
6+
<typeAliases>
7+
<package name="com.cisco.la.entity"/>
8+
</typeAliases>
9+
10+
<environments default="development">
11+
<environment id="development">
12+
<transactionManager type="JDBC" />
13+
<dataSource type="POOLED">
14+
<property name="driver" value="org.postgresql.Driver" />
15+
<property name="url" value="jdbc:postgresql://localhost:5432/la" />
16+
<property name="username" value="postgres" />
17+
<property name="password" value="19786028" />
18+
</dataSource>
19+
</environment>
20+
</environments>
21+
22+
<mappers>
23+
<mapper resource="com/cisco/la/dao/LaUserMapper.xml" />
24+
<mapper resource="com/cisco/la/dao/LaRlHistoryMapper.xml" />
25+
<mapper resource="com/cisco/la/dao/LaCrsHistoryMapper.xml" />
26+
</mappers>
27+
28+
29+
</configuration>
30+
31+
32+
33+
34+
35+
36+
37+

0 commit comments

Comments
 (0)