栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jdbcTemplate + spring + springmvc 整合案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jdbcTemplate + spring + springmvc 整合案例

jdbcTemplate + spring + springmvc 整合案例 超级详细滴案例

1.创建maven - webapp项目

项目名称xxxpom.xmlspringmvc_config.xmljdbc.propertiesweb.xmlpojo.User类daoservicecontrolindex.jspshow.jsp

1.创建maven - webapp项目

项目名称xxx

.ideasrc

main

java

com

controldaoservicepojo resources

springmvc_config.xmljdbc.properties webapp

WEB-INF

jsp

show.jsp web.xml index.jsp targetpom.xml pom.xml




 4.0.0

  com.baijie
  SpringmvcProject3.23
  1.0-SNAPSHOT
  war

  SpringmvcProject3.23 Maven Webapp
  
  http://www.example.com
  
  
  
  
   5.0.5.RELEASE
   UTF-8
   1.7
   1.7
   
   
   
   
   org.springframework
   spring-context
   ${spring.version}
   

   
    org.springframework
    spring-test
    5.0.5.RELEASE
    
    
    
    
    org.aspectj
    aspectjweaver
    1.8.13
    

    
    junit
    junit
    4.12
    
    
    
    
    
    c3p0
    c3p0
    0.9.1.2
    
    
    
    
    com.alibaba
    druid
    1.1.10
    

    
    
    mysql
    mysql-connector-java
    5.1.39
    
    
    
    c3p0
    c3p0
     0.9.1.2
     

     
     org.springframework
     spring-jdbc
     5.0.5.RELEASE
     
     
     
     org.springframework
     spring-tx
     5.0.5.RELEASE
     
     
     
     
     org.springframework
     spring-webmvc
     5.0.5.RELEASE
     
     
     
     
     javax.servlet
     servlet-api
     2.5
     
     
     
     
     javax.servlet.jsp
     jsp-api
     2.0
     
     

     
     SpringmvcProject3.23
     
     
     
     
     maven-clean-plugin
     3.1.0
     
     
     
     maven-resources-plugin
     3.0.2
     
     
     
     maven-compiler-plugin
     3.8.0
     
     
     
     maven-surefire-plugin
     2.22.1
     
     
     
     maven-war-plugin
     3.2.2
     
     
     
     maven-install-plugin
     2.5.2
     
     
     
     maven-deploy-plugin
     2.8.2
     
     
     
     
     

springmvc_config.xml


    
    

    
    

    
    
    
    
    
    
    
    
    
    
    
    

    
    
        
    

    
    
    
    

    
    

    
    
    
    

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

web.xml



  Archetype Created Web Application
  
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc_config.xml
    
    1
  
  
    DispatcherServlet
    /
  


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 + ''' +
                '}';
    }
}

dao
package baijie.dao;

import baijie.pojo.User;

import java.util.List;

public interface UserDao {
    //查询数据
    public List 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 findAll() {
        String sql="select * from user";
        List userList=jdbcTemplate.query(sql,
        new BeanPropertyRowMapper(User.class));
        return userList;
    }
}

service
package baijie.service;

import baijie.pojo.User;

import java.util.List;

public interface UserService {
    //查询数据
    public List 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 findAllUser() {
        return userDao.findAll();
    }
}

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(){
        ListallMethod=userService.findAllUser();
        System.out.println(allMethod);
        return "show";
    }
}

index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


点我wwwwwww

show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


sssssssssssssss

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号