您的位置:首页 > 编程语言 > Java开发

spring mvc +spring jdbc配置

2015-11-11 00:00 519 查看
先看一下项目的结构:



好了,下面讲解的时候就不说那个文件在那个目录下面了。

下面开始:
1:当然是在pom.xml中加入对jar的依赖。
<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.1.7.RELEASE</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.18</version>

</dependency>

我用的spring版本是4.1.7.为了方便,spring的所有部件版本我都用的4.1.7
2:为了使用spring mvc 我们需要在web.xml文件里面加一点配置。
<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

3:接着我们需要在web-inf目录下面加入spring-mvc的配置文件

<beans xmlns="http://www.springframework.org/schema/beans"

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

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

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

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

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

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

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<import resource="classpath:beans.xml"/>

<!-- 使Spring支持自动检测组件,如注解的Controller-->

<context:component-scan base-package="org.toyflivver.news.controller"/>

4:加入spring配置文件:

<beans

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

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

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

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

<bean id="newsDataSource"

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

<property name="driverClassName" value="com.mysql.jdbc.Driver">

</property>

<property name="url" value="jdbc:mysql://127.0.0.1:3306/flivvernews">

</property>

<property name="username" value="root"></property>

<property name="password" value="tangchaolizi123?"></property>

</bean>

<bean id="jdbcTemplate"

class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"

lazy-init="false" autowire="default">

<property name="dataSource">

<ref bean="newsDataSource" />

</property>

</bean>

</beans>

5:然后我们开始写代码:

@Controller(value="base")

@RequestMapping(value="base")

public class BaseController {

@Autowired(required=true)

private HttpServletRequest request;

@Autowired(required=true)

private HttpServletResponse response;

@Autowired(required=true)

private JdbcTemplate jdbcTemplate;

@RequestMapping(value="sayHello")

public void sayHello(String name) throws IOException{

PrintWriter out = response.getWriter();

String sql = "select t.id , t.name from t_user t";

List<Map<String, Object>> map = jdbcTemplate.queryForList(sql);

out.append("hello "+name+"!");

out.flush();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: