大学毕业快一年了,在学校里做了一个android APP的项目,一直都只是熟悉android后台开发是最大的短板,工作后,公司都是自己的框架,这种开源框架基本也没时间去接触。app和后台都是基于公司的平台开发,我觉得一个人做也没有啥难度。一直在混日子,把整个app的架构分析了一遍。后来公司业务需求,我被迫PC端和android客户端都的做。真心现在啥都不是研究的很深。心累。吐槽完毕。接下来,记录我自己学习ssm框架完成crud的整个过程。
点击完成工程就创建完毕。本文是以ssm为工程的如下:
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
*.action
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
SSM
contextConfigLocation
/WEB-INF/classes/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
创建spring相关的配置文件夹:
首先在config文件夹下创建如下文件:
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=mysql
log4j.properties
# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
然后在spring文件夹下配置如下文件
applicationContext-dao.xml
applicationContext-service.xml
applicationContext-transaction.xml
创建mybatis相关的文件夹
sqlMapConfig.xml
逆向工程生成po类和mapper接口
修改数据库连接
运行后生成的代码
为了生成动态代理对象,servce能自动注入需要确保如下配置。
编写service层类
Service层的方法需要注册到spring容器中
控制层调用service层的方法。通过modeandview返回数据给视图层(视图层通过addobject key取数据)。
调用控制层方法:
function queryItems(){
//提交form
document.itemsForm.action="${pageContext.request.contextPath }/items/getItemInfo.action?id=1";
document.itemsForm.submit();
}
<input type="button" value="查询" onclick="queryItems()"/>
http://localhost:8080/SSM/items/getItemInfo.action?id=1即可访问
至此环境搭建完成。
下面进行crud操作。
1、首先,需要建立一个test2数据库。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test2
jdbc.username=root
jdbc.password=admin
2、dao开发
便于扩展,定义如下三个类
package com.yqq.ssm.po;
import java.util.Date;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}
package com.yqq.ssm.po;
public class ItemsCustom extends Items {
}
package com.yqq.ssm.po;
public class ItemsCustomVO {
private Items items;
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
编写ItemCustomMapper.xmll映射文件
select LAST_INSERT_ID()
insert into items(name,price,pic,createtime,detail) values(#{itemsCustom.name},#{itemsCustom.price},
#{itemsCustom.pic},#{itemsCustom.createtime},#{itemsCustom.detail})
select uuid()
insert into items(name,price,pic,createtime,detail) values(#{itemsCustom.name},#{itemsCustom.price},#{itemsCustom.pic},#{itemsCustom.createtime},#{itemsCustom.detail})
update items set name=#{itemsCustom.name},price=#{itemsCustom.price},pic=#{itemsCustom.pic},createtime=#{itemsCustom.createtime},
detail=#{itemsCustom.detail} where id=#{itemsCustom.id}
update items set name=#{itemsCustom.name},price=#{itemsCustom.price},pic=#{itemsCustom.pic},createtime=#{itemsCustom.createtime},
detail=#{itemsCustom.detail} where id=#{itemsCustom.id}
delete from items where id=#{id}
定义ItemCustomMapper..java接口
package com.yqq.ssm.mapper;
import java.util.List;
import com.yqq.ssm.po.ItemsCustom;
import com.yqq.ssm.po.ItemsCustomVO;
public interface ItemCustomMapper {
int saveItems(ItemsCustomVO v) throws Exception;
int saveItemsUUID(ItemsCustomVO v) throws Exception;
List getAllItemsInfos() throws Exception;
void updItems(ItemsCustomVO v)throws Exception;
void delItembyId(int v)throws Exception;
}
编写service层代码
定义service接口,如下:
注册服务给controller使用:
控制层:
整个学习项目访问的地址:http://localhost:8080/SSM/items/getAllItemsInfos.action
代码下载地址:http://download.csdn.net/detail/u014600432/9527779