spring+springMVC+mybatis整合项目[2]

7 篇文章 0 订阅

整合mybatis

数据库链接

dataSource=org.apache.commons.dbcp.BasicDataSource

##Oracle
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
# if your oracle version is 12C then you should use this url
#url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#this key can not be "username" otherwise this value will be "Administrator",you should use nickname
name=phone 
paswd=phone

##MySQL driver
#com.mysql.jdbc.Driver
#jdbc:mysql://hostname:3306/dbname?useUnicode=true&characterEncoding=GBK 

数据库链接的spring配置文件,以及mybatis的相关

<?xml version="1.0" encoding="UTF-8"?>
<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" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	 
	 <!-- 读取jdbc配置文件 -->
	<context:property-placeholder location="classpath:config/jdbc.properties"/><!--加载配置文件 -->
	 
	 <!-- 读取配置文件中的数据库链接信息 -->
	 <bean id ="dataSource" class="${dataSource}">
	 	<property name="driverClassName" value="${driverClassName}"></property>
	 	<property name="url" value="${url}"></property>
	 	<property name="username" value="${name}"></property>
	 	<property name="password" value="${paswd}"></property>
	 </bean> 
	 
	 <!-- 配置sqlSessionFactoryBean,同时指定数据源-->
	 <bean class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<!-- 同时指定数据源 -->
	 	<property name="dataSource" ref="dataSource"></property>
	 	<!-- 指定映射文件 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml"/>
	 </bean>
	 
	 <!-- 配置mapperScannerConfigurer -->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<!-- 这个value必须指定到该类的包名 -->
	 	<property name="basePackage" value="com.pay.phone.dao"></property>
	 </bean>
</beans>

mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间和接口的类名相同 -->
<mapper namespace="com.pay.phone.dao.SysDao">

	<!-- 接口中的方法名称            返回类型,如果有传递参数,则传入相应的参数类型 -->
	<select id="getD" resultType="int">
		select count(*) from sys_action
	</select>
	
</mapper>

各个java类的代码很简单,现在主要是为了将项目的框架搭建好

controller类
package com.pay.phone.sys.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.pay.phone.sys.service.SysService;

@Controller
public class SysController {
	/************依赖注入***************/
	@Resource
	private SysService sysService;
	
	/****返回的jsp页面的名称****/
	private static final String LOGIN="login";
	
	@RequestMapping("/login.do")
	public ModelAndView getLogin() {
		System.out.println("login");
		System.out.println(sysService.loginService());
		return new ModelAndView(LOGIN);
	}
	
}
//Service层接口
package com.pay.phone.sys.service;

public interface SysService {
	public String loginService();
}
//service实现类
package com.pay.phone.sys.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pay.phone.dao.SysDao;

@Service
public class SysServiceImpl implements SysService{
	@Resource private SysDao sysDao;
	public String loginService() {
		return sysDao.getD().toString();
	}
}
//持久层
package com.pay.phone.dao;

import org.springframework.stereotype.Repository;

@Repository

public interface SysDao {
	Integer getD();
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>spring06-2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>spring</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<!-- 配置contextConfigLocation,spring加载的时候就不会去WEB-INF下寻找[servlet-name].sevlet.xml文件
  			 而是去配置好的路径下寻找配置的路径,如果找不到就会去WEB-INF下找[servlet-name].sevlet.xml文件
  			 如果路径配置错误或者找不到文件,则不会再项目启动的时候报错,会在访问项目的时候报错,因为DispathcherServlet会拦截
  			 所有以.do结尾的请求,这个时候回去寻找spring的配置文件找不到就会报错 -->
  		<param-name>contextConfigLocation</param-name>
  		<!-- <param-value>classpath:config/springmvc-servlet.xml</param-value> -->
  		<param-value>
  			/WEB-INF/classes/config/springmvc-servlet.xml,
  			/WEB-INF/classes/config/springmvc-jdbc.xml
  		</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值