mysql jdbc mevan springmvc_利用Intellij+MAVEN搭建SpringJDBC+MySql+SpringMVC项目详解【转载】...

4.0.0

SpringJDBCTest1

SpringJDBCTest1

war

1.0-SNAPSHOT

SpringJDBCTest1 Maven Webapp

http://maven.apache.org

org.springframework

spring-beans

4.3.1.RELEASE

org.springframework

spring-core

4.3.1.RELEASE

org.springframework

spring-context

4.3.1.RELEASE

org.springframework

spring-web

4.3.1.RELEASE

org.springframework

spring-webmvc

4.3.1.RELEASE

javax.servlet

jstl

1.2

taglibs

standard

1.1.2

javax.servlet

servlet-api

2.5

javax.servlet.jsp

jsp-api

2.2

mysql

mysql-connector-java

5.1.6

org.springframework

spring-jdbc

3.0.5.RELEASE

com.mchange

c3p0

0.9.5.1

org.aspectj

aspectjweaver

1.8.6

org.aspectj

aspectjrt

1.8.6

org.mybatis

mybatis

3.3.0

org.mybatis

mybatis-spring

1.2.3

SpringJDBCTest1

org.eclipse.jetty

jetty-maven-plugin

9.3.10.v20160621

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.2

true

true

org.apache.maven.plugins

maven-compiler-plugin

1.7

1.7

2 src下建java包和类:

实体类:Employee.java

package com.springjdbc.entities;

/*** Created by wanggenshen_sx on 2016/12/23.*/public class Employee {

private int id;

private String lastName;

private String email;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Override

public String toString() {

return "Employee [id=" + id + ", lastName=" + lastName + ", email="

+ email+" ]" ;

}

}

持久层类:EmployeeDao.java

这里注入JdbcTemplate,使用SpringJDBC框架。

package com.springjdbc.dao;

import com.springjdbc.entities.Employee;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.stereotype.Repository;

/*** Created by wanggenshen_sx on 2016/12/23.*/@Repository

public class EmployeeDao {

@Autowired

private JdbcTemplate jdbcTemplate;

public Employee get(int id){

String sql="SELECT id, lastName,email FROM employee WHERE id=?";

RowMapper rowMapper=new BeanPropertyRowMapper<>(Employee.class);

Employee employee=jdbcTemplate.queryForObject(sql, rowMapper,id);

return employee;

}

}

控制器:EmployeeController.java

package com.springjdbc.controller;

import com.springjdbc.dao.EmployeeDao;

import com.springjdbc.entities.Employee;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

import java.util.Map;

/*** Created by wanggenshen_sx on 2016/12/23.*/@Controller

public class EmployeeController {

private ApplicationContext ctx=null;

private JdbcTemplate jdbcTemplate=null;

private EmployeeDao employeeDao ;

private NamedParameterJdbcTemplate namedParameterJdbcTemplate=null;

{

ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

jdbcTemplate =(JdbcTemplate) ctx.getBean("jdbcTemplate");

employeeDao = ctx.getBean(EmployeeDao.class);

namedParameterJdbcTemplate=(NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate");

}

@RequestMapping(value = "/listAllEmployee",method = RequestMethod.GET)

public String list(Map map){

String sql="SELECT id, lastName,email FROM employee";

RowMapper rowMapper=new BeanPropertyRowMapper<>(Employee.class);

List employees=jdbcTemplate.query(sql, rowMapper);

map.put("employees",employees);

return "list";

}

}

3  配置sping和spingmvc的配置文件:

applicationContext.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

springmvc-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

4 配置web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

springmvc-servlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc-servlet

/

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

5 建显示页面list.jsp

Created by IntelliJ IDEA.

User: wanggenshen_sx

Date: 2016/12/23

Time: 16:56

To change this template use File | Settings | File Templates.

--%>

Show Page
IDLastNameEmail
${emp.id}${emp.lastName}${emp.email}

项目结构:

0acfbb2dc8b4b5cc60dd54ed695deeee.png

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值