概要
EJB&JNDI&MyBatisの使用例を備忘録として残しておきます。
アプリケーション構成


│
├─src
│ │ mybatis-config.xml
│ │
│ ├─resources
│ │ hoge.properties
│ │ sample_mapper.xml
│ │
│ └─sample
│ └─javaee
│ └─ejb
│ │ HelloData.java
│ │ HelloEjb.java
│ │ HelloProp.java
│ │
│ └─servlet
│ HelloDataServlet.java
│ HelloEjbServlet.java
│ HelloPropServlet.java
│
└─WebContent
│ index.jsp
│
└─WEB-INF
│ web.xml
│ weblogic.xml
│
└─lib
mybatis-3.5.3.jar
ソース
mybatis-config.xml
<?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>
<properties resource="resources/hoge.properties">
<property name="hoge" value="property init value"/>
<property name="fuga" value="property init value"/>
<property name="uwagaki" value="property init value"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="MANAGED"/>
<dataSource type="JNDI">
<property name="data_source" value="jdbc/oracle"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="resources/sample_mapper.xml" />
</mappers>
</configuration>
hoge.properties
fuga=property file naiyou1
zoyo=property file naiyou2
sample_mapper.xml
<?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="sample.mybatis">
<select id="selectTest" resultType="map">
select * from SAMPLEUSR1.PROPERTY
</select>
</mapper>
HelloEjbServlet.java
HelloEjb.java
HelloDataServlet.java
このブロックには、想定されていないか無効なコンテンツが含まれています。ブロックのリカバリーを試行
package sample.javaee.ejb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sample.javaee.ejb.HelloData;
@WebServlet("/helloData")
public class HelloDataServlet extends HttpServlet {
@EJB
private HelloData dt;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
List> result = this.dt.data();
PrintWriter out = null;
try {
out = resp.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.println("<html><head><title>Properties</title></head>");
out.println("<body>");
List wk = new ArrayList();
result.forEach(row -> {
System.out.println("---------------");
row.forEach((columnName, value) -> {
wk.add("columnName=" + columnName + ", value=" + value);
});
});
for (int i = 0; i < wk.size(); i++) {
out.println(wk.get(i));
}
out.println("</body></html>");
}
}
HelloData.java
このブロックには、想定されていないか無効なコンテンツが含まれています。ブロックのリカバリーを試行
package sample.javaee.ejb;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
@Stateless
public class HelloData {
@Resource(mappedName = "jdbc/oracle")
public DataSource ds;
public List> data() {
List> result = null;
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
System.out.println("You made it, take control your database now!");
// 設定ファイルを読み込む
try (InputStream in = HelloData.class.getResourceAsStream("/mybatis-config.xml")) {
// 設定ファイルを元に、 SqlSessionFactory を作成する
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// SqlSessionFactory から SqlSession を生成する
try (SqlSession session = factory.openSession()) {
// SqlSession を使って SQL を実行する
result = session.selectList("sample.mybatis.selectTest");
result.forEach(row -> {
System.out.println("---------------");
row.forEach((columnName, value) -> {
System.out.printf("columnName=%s, value=%s%n", columnName, value);
});
});
} catch (Exception e1) {
e1.printStackTrace();
}
} catch (Exception e2) {
e2.printStackTrace();
}
} else {
System.out.println("Failed to make connection!");
}
System.out.println("Hello Data!!");
return result;
}
}
HelloPropServlet.java
このブロックには、想定されていないか無効なコンテンツが含まれています。ブロックのリカバリーを試行
package sample.javaee.ejb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.ejb.EJB;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sample.javaee.ejb.HelloProp;
@WebServlet("/helloProp")
public class HelloPropServlet extends HttpServlet {
@EJB
private HelloProp pr;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
Properties prop = this.pr.data();
PrintWriter out = null;
try {
out = resp.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
out.println("<html><head><title>Properties</title></head>");
out.println("<body>");
List wk = new ArrayList();
prop.forEach((key, value) -> {
wk.add(key + "=" + value);
});
for (int i = 0; i < wk.size(); i++) {
out.println(wk.get(i));
}
out.println("</body></html>");
}
}
HelloProp.java
デプロイ




デバッグ







動作確認
HelloEjbServlet.java
http://localhost:7001/ejbTest/hello


HelloDataServlet.java
http://localhost:7001/ejbTest/helloData


HelloPropServlet.java
http://localhost:7001/ejbTest/helloProp

