ssm框架实现简单前后台交互
ssm框架实现简单交互
今天简单的测试了下在前端提交请求之后把对应的数据放在数据库中,
ssm(spring-springmvc-mybatis)实现过程可以是如下流程
来看下各个层的实现代码:
实体层:
package com.qcbylearn.entitys;public class person {private int a,b,c;public int getA() {return a;}public void setA(int a) {this.a = a;}public int getB() {return b;}public void setB(int b) {this.b = b;}public int getC() {return c;}public void setC(int c) {this.c = c;}}
dao层:
package com.qcbylearn.dao;import com.qcbylearn.entitys.person;public interface person_insert {int insert(person p); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ".dtd" > <mapper namespace="com.qcbylearn.dao.person_insert" > <insert id="insert" parameterType="com.qcbylearn.entitys.person" >insert into person values(#{a,jdbcType=BIGINT}, #{b,jdbcType=BIGINT}, #{c,jdbcType=BIGINT}) </insert></mapper>
其中的mapper映射dao层的interface接口,id对应接口里的方法。parameterType对应传入的参数,这里是一个类对象。
server层:
package com.qcbylearn.services;import com.qcbylearn.entitys.person;public interface person11 {int insert(person p);}
package com.qcbylearn.serviceimpl;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.qcbylearn.dao.person_insert; import com.qcbylearn.entitys.person; import com.qcbylearn.services.person11; @Service("tt") public class persontt implements person11{@Autowiredprivate person_insert person_insert;@Overridepublic int insert(person p) {person_insert.insert(p);return 0;} }
server层先实现server的接口,在实现的serverimpl中
在头部添加@webservice(tt),表示他是service层。
@Autowired
private person_insert person_insert;实现dao层xml配置的insert方法。
controllers层:
package com.qcbylearn.controllers;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import com.qcbylearn.entitys.person; import com.qcbylearn.services.person11;@Controller @RequestMapping("/test") public class test {@Autowiredprivate person11 tt;@ResponseBody@RequestMapping("/t1")public String test111(person p) {System.out.println("aaaaas");tt.insert(p);return "SUCCESS";}}
先声明@controller。
注意这个与上面的联系,private后面的类型是server的接口名称,tt是service的名称。
然后调用server的insert方法。从页面显示success,然后在数据库插入一条数据。
至此完成了简单的交互。
分析一下遇到的坑的地方:
- tomcat有时候无法启动,改了下spring-mvc.xml的xsd版本,可能是版本过低导入无法启动。
- 右击项目属性,要修改mvn下面的project facts dynamic web module的版本为3.0,java版本为1.8,dyplyment assembly里面引入mvn依赖。
- 有时候可能是tomcat或者项目有缓存,可以更新项目或者清除tomcat缓存试一试。
ssm框架实现简单前后台交互
ssm框架实现简单交互
今天简单的测试了下在前端提交请求之后把对应的数据放在数据库中,
ssm(spring-springmvc-mybatis)实现过程可以是如下流程
来看下各个层的实现代码:
实体层:
package com.qcbylearn.entitys;public class person {private int a,b,c;public int getA() {return a;}public void setA(int a) {this.a = a;}public int getB() {return b;}public void setB(int b) {this.b = b;}public int getC() {return c;}public void setC(int c) {this.c = c;}}
dao层:
package com.qcbylearn.dao;import com.qcbylearn.entitys.person;public interface person_insert {int insert(person p); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ".dtd" > <mapper namespace="com.qcbylearn.dao.person_insert" > <insert id="insert" parameterType="com.qcbylearn.entitys.person" >insert into person values(#{a,jdbcType=BIGINT}, #{b,jdbcType=BIGINT}, #{c,jdbcType=BIGINT}) </insert></mapper>
其中的mapper映射dao层的interface接口,id对应接口里的方法。parameterType对应传入的参数,这里是一个类对象。
server层:
package com.qcbylearn.services;import com.qcbylearn.entitys.person;public interface person11 {int insert(person p);}
package com.qcbylearn.serviceimpl;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.qcbylearn.dao.person_insert; import com.qcbylearn.entitys.person; import com.qcbylearn.services.person11; @Service("tt") public class persontt implements person11{@Autowiredprivate person_insert person_insert;@Overridepublic int insert(person p) {person_insert.insert(p);return 0;} }
server层先实现server的接口,在实现的serverimpl中
在头部添加@webservice(tt),表示他是service层。
@Autowired
private person_insert person_insert;实现dao层xml配置的insert方法。
controllers层:
package com.qcbylearn.controllers;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import com.qcbylearn.entitys.person; import com.qcbylearn.services.person11;@Controller @RequestMapping("/test") public class test {@Autowiredprivate person11 tt;@ResponseBody@RequestMapping("/t1")public String test111(person p) {System.out.println("aaaaas");tt.insert(p);return "SUCCESS";}}
先声明@controller。
注意这个与上面的联系,private后面的类型是server的接口名称,tt是service的名称。
然后调用server的insert方法。从页面显示success,然后在数据库插入一条数据。
至此完成了简单的交互。
分析一下遇到的坑的地方:
- tomcat有时候无法启动,改了下spring-mvc.xml的xsd版本,可能是版本过低导入无法启动。
- 右击项目属性,要修改mvn下面的project facts dynamic web module的版本为3.0,java版本为1.8,dyplyment assembly里面引入mvn依赖。
- 有时候可能是tomcat或者项目有缓存,可以更新项目或者清除tomcat缓存试一试。