初次整合ssm

本人实属菜鸟,大佬勿喷

简单整合一下SSM实现crud


直接上代码了,接着本人上次springMVC-crud写的,没什么新颖点

配置
applicationContext.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: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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 读取jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 创建DataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="${jdbc.url}"/>
		<property name="driverClassName" value="${jdbc.driverClass}"/>
		<property name="username" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="maxActive" value="10"/>
		<property name="maxIdle" value="5"/>
	</bean>	
	
	<!-- 创建SqlSessionFactory对象 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 关联连接池 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 加载sql映射文件 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml"/>
	</bean>

	<!-- Mapper接口的扫描 -->
	<!-- 
		注意:如果使用Mapper接口包扫描,那么每个Mapper接口在Spring容器中的id名称为类名: 例如 CustomerMapper -> customerMapper
	 -->

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置Mapper接口所在包路径  -->
		<property name="basePackage" value="cn.lm1234.dao"/>
	</bean>
	
	
	
	<!-- 开启Spring的IOC注解扫描 -->
	<context:component-scan base-package="cn.lm1234"/>

	
	<!-- 开启Spring的事务 -->
	<!-- -事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 启用Spring事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

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" id="WebApp_ID" version="2.5">
  <display-name>01.mybatis</display-name>
  
  	<!-- 配置SpringMVC编码过滤器 -->
  	<filter>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<param-name>encoding</param-name>
  			<param-value>utf-8</param-value>
  		</init-param>
  	</filter>
  	<filter-mapping>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<url-pattern>/ *</url-pattern> 
  	</filter-mapping>
  

	<!-- 启动SpringMVC -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 参数:读取spring-mvc.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

  
  	<!-- 启动spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 修改路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

studentmapper.xml

<?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">
<!-- 该文件编写mybatis中的mapper接口里面的方法提供对应的sql语句 -->
<mapper namespace="cn.lm1234.dao.StudentMapper">
   
    <select id="selectAl" resultType="cn.lm1234.domain.Student">
      select * from student;
    </select>
       
     <select  id="selectById"   resultType="cn.lm1234.domain.Student"   parameterType="int">
        select * from student where stuno=#{stuno}
     </select>
       
       <update id="update" parameterType="cn.lm1234.domain.Student">
     update  student set stuname=#{stuName},stuage=#{stuAge},graname=#{graName} where  stuno=#{stuNo}
    </update>
    
     <insert id="addStudent"    parameterType="cn.lm1234.domain.Student">
    insert into student(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName})
    </insert>
    
    <delete id="deleteById"   parameterType="int">
      delete from student where stuno=#{stuNo};
    </delete>
</mapper>

简单实现
controller

@Controller
public class StudentController {
	@Autowired
    StudentService  studentService;
		
	@RequestMapping("/input")
	public   String list(Model model){
		//查询数据
		Collection<Student> all= studentService.selectAll();
		model.addAttribute("emps", all);
		return "list";	
	}
	
	@RequestMapping(value="/emp/{stuNo}",method=RequestMethod.GET)
	public String SelectById(@PathVariable("stuNo")Integer stuNo,Model model)
	{
		      Student student = studentService.selectById(stuNo);
		      model.addAttribute("student", student);
		      return  "edit";
	}
   
	@RequestMapping(value="/emp/{stuNo}",method=RequestMethod.POST)
	public String updateEmp(@ModelAttribute("student")Student student)
	{
		System.out.println("要修改的员工"+student);
		studentService.update(student);
		return "index";
	}
	
	@RequestMapping("toaddpage")
	public String toAddPage(Model model)
	{
		  model.addAttribute("student",new Student() );
		//3 去添加页面
 		return "add";
	}
	
	@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String up(@ModelAttribute("student")Student student)
	{
		System.out.println("要添加的员工"+student);
		studentService.addStudent(student);
		return "redirect:/input";
	}
	
	@RequestMapping(value="/emps/{id}",method=RequestMethod.POST)
	public String deleteEmp(@PathVariable("id")Integer id)
	{
		studentService.delete(id);
		return "redirect:/input";
	}
	
	@RequestMapping("/anotherinput")
	@ResponseBody
	public   List<Student> ss (){
		//查询数据
		List<Student> all= studentService.selectAll();
		return all;	
	}
}

index.jsp

<jsp:forward page="/input" />

list.jsp 查出所有学生

<body>
<h1>员工列表</h1>
	<table border="1" cellpadding="5" cellspacing="0">
		<tr>
			<th>stuNo</th>
			<th>stuName</th>
			<th>stuAge</th>
			<th>graName</th>
			<th>Edit</th>
			<th>delete</th>
		</tr>


		<c:forEach items="${emps}" var="emp">
		<tr>
			<td>${emp.stuNo }</td>
			<td>${emp.stuName}</td>
			<td>${emp.stuAge }</td>
			<td>${emp.graName }</td>			
			<td>
			<a href="${ctp}/emp/${emp.stuNo }">Edit</a>
			</td>
			<td>
			<form action="${ctp}/emps/${emp.stuNo }"  method="POST">
			<input type="hidden" name="_method" value="DELETE"/> <br/>
	         <input type="submit" value="删除"/><br/> 
			</form>
			</td>
		</tr>
	</c:forEach>
	</table>
	<a href="${ctp }/toaddpage">添加员工</a>
</body>

增加 add.jsp

<body>
   <%
      pageContext.setAttribute("ctp", request.getContextPath());
    %>
<form:form action="${ctp}/emp "  modelAttribute="student" method="POST">
       <!--path:就是原来html-input的name项 : 需要写  
          path:
          1) 当作原生的name项
          2)自动回显隐含模型中某个对象对应这个属性的值
       -->
      lastName:<form:input path="stuName"/>
      stuAge:<form:input path="stuAge"/>
      graName:<form:input path="graName"/>
      <input type="submit" value="提交">
</form:form>
</body>

修改
edit.jsp

<body>
  <%
      pageContext.setAttribute("ctp", request.getContextPath());
    %>
    
    <h1>学生修改页面</h1>
    
    <form:form  action="${ctp}/emp/${student.stuNo }"  modelAttribute="student" method="POST">
    <input type="hidden" name="_method" value="put"/>   <!-- 我们需要提交的方式 post->put -->
     <input type="hidden" name="stuno" value="${student.stuNo}"/>
       stuName:<form:input path="stuName"/>
        stuage:<form:input path="stuAge"/>
        graname:<form:input path="graName"/>
        <input type="submit"  value="修改">
    </form:form>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值