1. 程式人生 > 其它 >jdbcTemplate + spring + springmvc 整合案例

jdbcTemplate + spring + springmvc 整合案例

jdbcTemplate + spring + springmvc 整合案例 超級詳細滴案例

1.建立maven - webapp專案 

2.專案名稱xxx

3.pom.xml

<?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.baijie</groupId>
  <artifactId>SpringmvcProject3.23</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringmvcProject3.23 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  
  <properties>
  
  <!--統一設定spring框架相關jar的版本號-->
   <spring.version>5.0.5.RELEASE</spring.version>
   <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.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
   </dependency>

   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.5.RELEASE</version>
    </dependency>
    
    <!-- aspectj的織入(aop座標) -->
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
    </dependency>

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <!--<scope>test</scope>-->
    </dependency>
    
    <!-- C3P0連線池 -->
    <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
    </dependency>
    
    <!-- Druid連線池 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
    </dependency>

    <!-- mysql驅動 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
    </dependency>
    
    <dependency>
    <grou

4.springmvc_config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置註解掃描-->
    <context:component-scan base-package="baijie"/>

    <!--讀取外部jdbc的檔案-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--c3p0配置spring連線池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${driver}"/>
    <property name="jdbcUrl" value="${url}"/>
    <property name="user" value="${user}"/>
    <property name="password" value="${password}"/>
    
    <!--配置連線池常用屬性-->
    <property name="initialPoolSize" value="${initSize}"/>
    <property name="maxPoolSize" value="${maxSize}"/>
    <property name="minPoolSize" value="${minSize}"/>
    </bean>

    <!--配置jdbc模板物件-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事務管理器物件-->
    <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置內部資源檢視解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <!--  /jsp/success.jsp  -->
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>

    <!--掃描元件:掃描哪些包下面的類添加了註解-->
    <context:component-scan base-package="baijie"></context:component-scan>
    </beans>

5.jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/baijie
user=root
password=123456
initSize=10
minSize=20
maxSize=1000

6.web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--配置前端控制器+springMVC配置檔案-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc_config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

7.pojo.User類

package baijie.pojo;

public class User {
    private int id;
    private String username;
    private String password;

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

8.dao

package baijie.dao;

import baijie.pojo.User;

import java.util.List;

public interface UserDao {
    //查詢資料
    public List<User> findAll();
}

package baijie.dao;

import baijie.pojo.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
@Repository("userDao")
public class UserDaoImpl implements UserDao{

    //注入jdbcTemplate物件
    @Resource
    private JdbcTemplate jdbcTemplate;
    //查詢所有資料
    @Override
    public List<User> findAll() {
        String sql="select * from user";
        List<User> userList=jdbcTemplate.query(sql,
        new BeanPropertyRowMapper<User>(User.class));
        return userList;
    }
}

9.service

package baijie.service;

import baijie.pojo.User;

import java.util.List;

public interface UserService {
    //查詢資料
    public List<User> findAllUser();
}
package baijie.service;

import baijie.dao.UserDao;
import baijie.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)//配置公共的事務特性
public class UserServiceImpl implements UserService{
    //呼叫Dao層
    @Autowired
    private UserDao userDao;
   //查詢所有資料
    @Override
    public List<User> findAllUser() {
        return userDao.findAll();
    }
}

10.control

package baijie.control;

import baijie.dao.UserDao;
import baijie.pojo.User;
import baijie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserControl {
    @Resource
    private UserService userService;

    //查詢所有
    @RequestMapping(value = "/showUser")
    public String showUserMethod(){
        List<User>allMethod=userService.findAllUser();
        System.out.println(allMethod);
        return "show";
    }
}

11.index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2><a href="user/showUser">點我wwwwwww</a></h2>
</body>
</html>

12.show.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>sssssssssssssss</h1>
</body>
</html>