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采用注解的写法。