spring+springmvc+mybatis框架注解版搭建

本文详细介绍SSM(Spring+SpringMVC+MyBatis)框架的搭建过程与配置方法,包括web.xml、bean.xml和conf.xml等核心配置文件的内容及解释,并通过具体代码示例展示了如何使用这些配置。
摘要由CSDN通过智能技术生成

ssm框架搭建配置文件如下图所示


主要配置web.xml, bean.xml,conf.xml下面是代码展示

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/bean.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>*.action</url-pattern>
  </servlet-mapping>
</web-app>
bean.xml

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/pubs10" />
		<property name="username" value="root" />
		<property name="password" value="mysql" />
    </bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:conf.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
    <!-- <bean id="user1Dao" class="it.com.dao.User1Dao" autowire="byType">
      <property name="sf" ref="sqlSessionFactory"></property>
    </bean> -->
</beans>

conf.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<mappers>
      <mapper class="it.com.dao.IusersDao"></mapper>
</mappers>
    
</configuration>

mybatis  注解接口

package it.com.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface IusersDao {
	@Select("select * from authors")
	public List<Map> findAll();
	@Select("select username,userpwd,email,logo from users")
	public List<Map> selectAllUser();
	@Update("update users set userpwd=#{userpwd},email=#{email},logo=#{logo} where username=#{username}")
	public void updateUsers(@Param("username") String username,@Param("userpwd") String userpwd,@Param("email") String email,@Param("logo") String logo);
}
实现类

package it.com.dao;


import java.util.List;
import java.util.Map;


import javax.annotation.Resource;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
@Repository
public class User1Dao {
	@Resource
	SqlSessionFactory sf;
	public List<Map> findAll(){
		SqlSession session = sf.openSession();
//		IusersDao id =sf.openSession().getMapper(IusersDao.class);
//		List list= id.findAll();
		List<Map> list =session.getMapper(IusersDao.class).findAll();
		return list;
	}
	public SqlSessionFactory getSf() {
		return sf;
	}
	public void setSf(SqlSessionFactory sf) {
		this.sf = sf;
	}
	
	public List<Map> findAllUser(){
		SqlSession session = sf.openSession();
		List<Map> list = session.getMapper(IusersDao.class).selectAllUser();
		return list;
	}
	
	public void updateUsers(String username,String userpwd,String email,String logo){
		SqlSession session =sf.openSession();
		session.getMapper(IusersDao.class).updateUsers(username, userpwd, email, logo);
	}
}



action注解,相当于原先的severlet

package it.com.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import it.com.dao.User1Dao;

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

@Controller
public class Usersaction {
@Resource
   User1Dao ud;
@RequestMapping(value="selectUsersAll.action")
   public ModelAndView selectUsersAll(HttpServletRequest request,HttpServletResponse response){
	   List<Map> list=ud.findAll();
	   //System.out.println(list);
	   //request.getSession().setAttribute("list", list);
	   return new ModelAndView("show.jsp","list",list);
   }
@RequestMapping(value="selectAllUser.action")
   public ModelAndView selectAllUser(HttpServletRequest request,HttpServletResponse response){
	List<Map> list = ud.findAllUser();
	return new ModelAndView("x1.jsp","list",list);
  }

@RequestMapping(value="updateusers.action")
   public ModelAndView updateusers(HttpServletRequest request,HttpServletResponse response){
   String username=request.getParameter("username");
   String userpwd = request.getParameter("userpwd");
   String email = request.getParameter("email");
   String logo = request.getParameter("logo");
   
   System.out.println(username+""+userpwd+""+email+""+logo);
   ud.updateUsers(username, userpwd, email, logo);
   List<Map> list = ud.findAllUser();
   return new ModelAndView("x1.jsp","list",list);
}
}

上面代码中猴头标记的部分相当于配置文件中bean的bytape和byname,如果配置文件中写了,就不需要在这里写了。个人建议mybatis采用配制文件写法,spring action采用注解的写法。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值