启动tomcat服务器,在index.jsp中代码使用datasource读取数据
mybatis-jndi
1.pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>day03_eesy_05jndi</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>day03_eesy_05jndi Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version></dependency></dependencies><build><finalName>day03_eesy_05jndi</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.20.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.0</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
2.IUserDao
package com.lq.dao;import com.lq.domain.User;import java.util.List;public interface IUserDao {
List<User> findAll();}
3.User
package com.lq.domain;import java.io.Serializable;
import java.util.Date;/*** @author 黑马程序员* @Company http://www.ithiema.com*/
public class User implements Serializable {
private Integer userId;private String userName;private String userAddress;private String userSex;private Date userBirthday;public Integer getUserId() {
return userId;}public void setUserId(Integer userId) {
this.userId = userId;}public String getUserName() {
return userName;}public void setUserName(String userName) {
this.userName = userName;}public String getUserAddress() {
return userAddress;}public void setUserAddress(String userAddress) {
this.userAddress = userAddress;}public String getUserSex() {
return userSex;}public void setUserSex(String userSex) {
this.userSex = userSex;}public Date getUserBirthday() {
return userBirthday;}public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;}@Overridepublic String toString() {
return "User{" +"userId=" + userId +", userName='" + userName + '\'' +", userAddress='" + userAddress + '\'' +", userSex='" + userSex + '\'' +", userBirthday=" + userBirthday +'}';}
}
4.IUserDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lq.dao.IUserDao"><!-- 配置 查询结果的列名和实体类的属性名的对应关系 --><resultMap id="userMap" type="uSeR"><!-- 主键字段的对应 --><id property="userId" column="id"></id><!--非主键字段的对应--><result property="userName" column="username"></result><result property="userAddress" column="address"></result><result property="userSex" column="sex"></result><result property="userBirthday" column="birthday"></result></resultMap><!-- 查询所有 --><select id="findAll" resultMap="userMap"><!--select id as userId,username as userName,address as userAddress,sex as userSex,birthday as userBirthday from user;-->select * from user;</select></mapper>
5.SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入约束 -->
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><typeAliases><package name="com.lq.domain"></package></typeAliases><!-- 配置mybatis的环境 --><environments default="mysql"><!-- 配置mysql的环境 --><environment id="mysql"><!-- 配置事务控制的方式 --><transactionManager type="JDBC"></transactionManager><!-- 配置连接数据库的必备信息 type属性表示是否使用数据源(连接池)--><dataSource type="JNDI"><property name="data_source" value="java:comp/env/jdbc/eesy_mybatis"/></dataSource></environment></environments><!-- 指定mapper配置文件的位置 --><mappers><mapper resource="com/lq/dao/IUserDao.xml"/></mappers>
</configuration>
6.context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!--
<Resource
name="jdbc/eesy_mybatis" 数据源的名称
type="javax.sql.DataSource" 数据源类型
auth="Container" 数据源提供者
maxActive="20" 最大活动数
maxWait="10000" 最大等待时间
maxIdle="5" 最大空闲数
username="root" 用户名
password="1234" 密码
driverClassName="com.mysql.jdbc.Driver" 驱动类
url="jdbc:mysql://localhost:3306/eesy_mybatis" 连接url字符串
/>-->
<Resource
name="jdbc/eesy_mybatis"
type="javax.sql.DataSource"
auth="Container"
maxActive="20"
maxWait="10000"
maxIdle="5"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/eesy"
/>
</Context>
7.index.jsp
<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.ibatis.io.Resources" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.lq.dao.IUserDao" %>
<%@ page import="com.lq.domain.User" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<h2>Hello World!</h2>
<%//1.读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");//2.根据配置文件构建SqlSessionFactorySqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3.使用SqlSessionFactory创建SqlSession对象SqlSession sqlSession = factory.openSession();//4.使用SqlSession构建Dao的代理对象IUserDao userDao = sqlSession.getMapper(IUserDao.class);//5.执行dao中的findAll方法List<User> users = userDao.findAll();for(User user : users){
System.out.println(user);}//6.释放资源sqlSession.close();in.close();
%>
</body>
</html>