springboot(SSM)综合案例

SpringBoot案例笔记(基于eclipse开发的SSM综合案例)

Spring框架、SpringMVC框架、Mybatis框架。

SpringBoot:简单快速【创建配置Spring应用的框架】。

Spring:基于【IOC原理】的【JavaEE】框架。

SpringMVC:基于【MVC】的【web】框架。(和前端交互的那一层)

Mybatis:基于【ORM】的【持久层】框架。

控制层业务层数据层企业信息系统层
ControllerServiceDAOEIS
SpringBootSpringBootSpringBootMysql(SQL)
SpringSpringSpring
SpringMVCJavaMybatis
ComActionComServiceComcom表
ComServiceImplComDAO
ComMapper.xml
EmpActionEmpServiceEmpemp表
EmpServiceImplEmpDAO
EmpMapper.xml

综合案例

(一)EIS层开发

(1)创建数据库db0423
(2)创建公司表com和员工表emp
(3)添加数据

(二)创建配置SpringBoot工程

(1)创建SpringBoot工程(工程名springboot04、Group和package:com.名字全拼)
(2)Spring、SpringMVC、Mysql、Mybatis(Mybatis FrameWork,MySQL Driver,spring web)
(3)pom.xml添加maven插件版本、maven更新。
(4)配置application.properties(tomcat、mysql、mybatis)

#tomcat配置端口
server.port=8080
#mysql数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mysql数据库地址(注意修改数据库名字db0423)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db0423?characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
#mysql数据库用户
spring.datasource.username=root
#mysql数据库密码
spring.datasource.password=123456
#mybatis框架SQL映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis实体类的包名
mybatis.type-aliases-package=com.cuiyudong.bean

(5)配置pom.xml(SpringMVC、Mybatis、Mysql、PageHelper)

<dependencies>
		<!-- (1)SpringMVC框架 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- (2)Mybatis框架 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.4</version>
		</dependency>

		<!-- (3)Mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		
	   <!-- pagehelper分页插件配置 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

(三)DAO层开发(数据层)

(1)实体类
com.cuiyudong.bean.Com
public class Com {
	Integer id;
	String name;
	public Com(String name) {//注意:由于id在数据库中已经设置了自动递增,所以在生成有参的方法时不需要勾选id,员工类同理
		super();
		this.name = name;
	}
}
com.cuiyudong.bean.Emp
public class Emp {
	Integer id;
	String name;
	Integer comid;
	Com com;//注意:在员工表中要把公司对象封装起来,因为查询员工的信息会有公司信息
}
(2)设计DAO接口

注意:在DAO层中只有接口,接口中定义方法,没有实体类。mybatis通过代理模式自动实现这个接口去实现方法。

com.zhaoyang.dao.ComDAO
//公司接口,生成五个方法(默认被public abstract修饰)
public interface ComDAO {
	int insert(Com com);//返回类型是整型,表示增加的条数
	int update(Com com);//返回类型是整型,表示修改的条数
	int delete(int id);//根据公司id删除一个公司信息
	Com getOne(int id);//根据公司id查询一个公司信息,返回类型是Com类型
	List<Com> getAll(String name);//根据公司一个公司名字模糊查询相关所有信息
}
com.zhaoyang.dao.EmpDAO(EmpDAO同ComDAO)
(3)实现DAO接口(SQLMapper.xml映射文件)
mapper/ComMapper.xml
<mapper namespace="com.cuiyudong.dao.ComDAO">  //com.cuiyudong.dao.ComDAO表示要实现的接口
<insert id="insert" parameterType="Com">//id后面是方法名,parameterType是表示输入的参数类型(方法参数)
insert into com(name) values(#{name})
</insert>

<update id="update" parameterType="Com">
update com set name=#{name} where id=#{id}
</update>

<delete id="delete" parameterType="int">
delete from com where id=#{id}
</delete>

<select id="getOne" parameterType="int" resultType="Com">//resultType表示返回类型,返回的是一个Com类型,只适用单表,因为一个表对应一个类。类中的变量也是和表中的字段相对应,这个时候Mybatis框架会自动封装
select * from com where id = #{id}
</select>
//注意:List<Com> 返回类型是Com
<select id="getAll" parameterType="string" resultType="Com">
select * from com where name like concat('%',#{name},'%')
</select>
mapper/EmpMapper.xml
<mapper namespace="com.zhaoyang.dao.EmpDAO">  
<!-- resultMap:结果映射:配置对象关系映射 -->
<resultMap type="Emp" id="map01">

//查询最后返回的是员工对象,所以type="Emp",id表示映射的名字,随便起

<id property="id" column="eid"/> //表示员工类中id与eid是一组映射

//第一个标签是id,也是最特殊的,只有他用id标签表示,property表示所对应的实体类中的变量(属性名)
//column表示【查询结果的字段名】
//其余标签用result
<result property="name" column="ename"/>
<result property="com.id" column="cid"/>
<result property="com.name" column="cname"/>
</resultMap>

//注意:我们需要查询些什么,就在resultMap中映射什么,如果没有,在视图层会显示“null”

<insert id="insert" parameterType="Emp">
insert into emp(name,comid) values(#{name},#{comid})
</insert>

<update id="update" parameterType="Emp">
update emp set name=#{name}, comid=#{comid} where id=#{id}
</update>

<delete id="delete" parameterType="int">
delete from emp where id=#{id}
</delete>

<select id="getOne" parameterType="int" resultMap="map01"> //resultMap="map01"是映射名字,表示查询完之后,用map01的映射封装

//由于员工中的信息有公司信息,这个时候需要多表查询。由于员工表和公司表都有id和name,这个时候就需要起别名来区分开
//员工表的外键不用在select查询这里显示
select emp.id eid, emp.name ename, com.id cid, com.name cname 
from emp, com 
where emp.comid = com.id and emp.id = #{id}  
//emp.comid = com.id表示公共列,“emp.id = #{id}”表示写的方法是根据员工id查询信息
</select>

<select id="getAll" parameterType="string" resultMap="map01">
select emp.id eid, emp.name ename, com.id cid, com.name cname 
from emp, com 
where emp.comid = com.id and emp.name like concat('%',#{name},'%')
//“emp.name like concat('%',#{name},'%')”表示根据员工名字模糊查询
</select>

(四)Service层开发(业务层,看功能的实现)

说明一:业务层是项目核心层、最重要的层。

说明二:业务层要对数据层的方法再次封装。

说明三:业务层要对数据层的方法进行优化。(更关注用户需求,是看着页面写的)

(1)设计Service接口
com.zhaoyang.service.ComService
//业务层的接口方法和DAO层一样
public interface ComService {
	int add(Com com);
	int edit(Com com);
	int remove(int id);
	Com findOne(int id);
    //注意,业务层的模糊查询参数列表中要有分页,pageNum表示第几页,pageSize表示显示几条
	List<Com> findAll(String name, int pageNum, int pageSize);
}
com.zhaoyang.service.EmpService(EmpService同ComService)
(2)实现Service接口
com.zhaoyang.service.ComServiceImpl(公司业务层实现类)
@Service //依赖注入
public class ComServiceImpl implements ComService {
	@Autowired//相当于等号,赋值,把dao的对象赋值给变量【在IoC容器自动查找需要的bean,并装配给该对象的属性】
	ComDAO dao;//ComServiceImpl公司的业务实现,调用的是公司的数据层ComDAO dao,创建对象,注入ComDAO的底层数据
	public int add(Com com) {
		int i = dao.insert(com);//方法的实现,业务层调DAO层
		return i;
	}
	public int edit(Com com) {
		int i = dao.update(com);
		return i;
	}
	public int remove(int id) {
		int i = dao.delete(id);
		return i;
	}
	public Com findOne(int id) {
		Com c = dao.getOne(id);
		return c;
	}
	public List<Com> findAll(String name, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Com> list = dao.getAll(name);
		return list;
	}
}
com.zhaoyang.service.EmpServiceImpl(EmpServiceImpl同ComServiceImpl

(五)Controller层开发(控制层)

(1)控制器
com.zhaoyang.controller.ComAction
@RestController
@RequestMapping("com")
public class ComAction {
	@Autowired
	ComService service;
	@RequestMapping("m1")
	public int m1(Com com) {
		return service.add(com);
	}
	@RequestMapping("m2")
	public int m2(Com com) {
		return service.edit(com);
	}
	@RequestMapping("m3")
	public int m3(int id) {
		return service.remove(id);
	}
	@RequestMapping("m4")
	public Com m4(int id) {
		return service.findOne(id);
	}
	@RequestMapping("m5")
	public List<Com> m5(String name, int pageNum, int pageSize) {
		return service.findAll(name, pageNum, pageSize);
	}
}
com.zhaoyang.controller.EmpAction(EmpAction同ComAction

(六)启动类添加dao接口扫描包、启动服务器、测试控制器中的方法

//发现你的
@MapperScan("com.cuiyudong.dao")
public class Springboot04Application {

添加
http://127.0.0.1:8080/com/m1?name=dell
修改
http://127.0.0.1:8080/com/m2?name=google&id=3
删除
http://127.0.0.1:8080/com/m3?id=3
查询
http://127.0.0.1:8080/com/m4?id=1
http://127.0.0.1:8080/com/m5?name=b&pageNum=1&pageSize=1

添加
http://127.0.0.1:8080/emp/m1?name=abc&comid=1
修改
http://127.0.0.1:8080/emp/m2?name=xyz&comid=2&id=5
删除
http://127.0.0.1:8080/emp/m3?id=5
查询
http://127.0.0.1:8080/emp/m4?id=1
http://127.0.0.1:8080/emp/m5?name=张&pageNum=1&pageSize=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YD_1989

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值