SSM
- SSM
- 需要配置的文件
- 配置applicationContext.xml
- 配置database.properties
- 配置mappers/ExamDao.xml
- 在java目录下创建controller、dao、pojo、service目录
- 控制类
- 接口类(dao)
- 实体类(pojo)
- 服务层service
- service接口类
- 服务层实现类
SSM
SSM包含框架
spring
springMVC 层级MODEL VIEW Controller
mybatis 层级DB
其他
Servlet jdbc
springcloud —>微服务
—> 1 alibaba —> docker —> jen
需要配置的文件
配置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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!--包的注解扫描-->
<context:component-scan base-package="nj.zb.kb21"></context:component-scan>
<!-- 数据库配置信息 -->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--加载数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${mysqlurl}"/>
<property name="driverClass" value="${mysqldriver}"/>
<property name="user" value="${mysqluser}"/>
<property name="password" value="${mysqlpwd}"/>
</bean>
<!-- 配置SqlSessionFactory工厂对象 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--扫描实体类包pojo-->
<property name="typeAliasesPackage" value="nj.zb.kb21.pojo"></property>
<!--扫描Sql配置文件(dao接口的实现xml文件)-->
<property name="mapperLocations" value="classpath:mappers/ExamDao.xml"></property>
</bean>
<!-- 完成映射类 -->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<property name="basePackage" value="nj.zb.kb21"></property>
</bean>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<mvc:annotation-driven/>
</beans>
配置database.properties
mysqldriver=com.mysql.cj.jdbc.Driver
mysqlurl=jdbc:mysql://192.168.153.141:3306/exam?allowMultiQueries=true
mysqluser=root
mysqlpwd=root
配置mappers/ExamDao.xml
在resources下创建mappers/ExamDao.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">
<mapper namespace="nj.zb.kb21.dao.ExamDao">
<select id="getAllStudent" resultType="student">
select s.id,s.name,s.age,s.gender,s.telephone,s.email,s.classid ,c.name className
from exam.student s left join classes c
on s.classid = c.id;
</select>
<insert id="addStudent">
insert into exam.student(id,name,age,gender,telephone,email,classId)
values (null,#{name},#{age},#{gender},#{telephone},#{email},#{classId})
</insert>
<select id="getAllClasses" resultType="classes">
select id,name from exam.classes;
</select>
</mapper>
在java目录下创建controller、dao、pojo、service目录
控制类
@Controller
public class ExamController {
@Autowired
private ExamService examService;
//http://localhost:8080/loadstu.do
@RequestMapping("loadstu")
@ResponseBody //将方法返回的对象转换成JSON字符串
public List<Student> getStus(){
List<Student> allStudent = examService.getAllStudent();
return allStudent;
};
//http://localhost:8080/loadcls.do
@RequestMapping("loadcls")
@ResponseBody
public List<Classes> getCls(){
List<Classes> allClasses = examService.getAllClasses();
return allClasses;
}
//http://localhost:8080/savestu.do
@RequestMapping("savestu")
@ResponseBody
public Integer saveStudent(Student stu){
System.out.println(stu);
Integer integer = examService.addStudent(stu);
return integer;
}
}
接口类(dao)
public interface ExamDao {
List<Student> getAllStudent();
List<Classes> getAllClasses();
Integer addStudent(Student stu);
}
实体类(pojo)
对应数据库中的词条 描述set get方法的类
服务层service
service接口类
public interface ExamService {
/**
* 查询所有学生信息及班级信息
* @return
*/
public List<Student> getAllStudent();
/**
* 添加新的学生信息
* @param student
* @return
*/
public Integer addStudent(Student student);
/**
* 获取所有班级信息
* @return
*/
public List<Classes> getAllClasses();
}
服务层实现类
/**
* 服务层 Service
*/
@Service
@Transactional
public class ExamServiceImpl implements ExamService {
@Autowired
private ExamDao examDao;
@Override
public List<Student> getAllStudent() {
return examDao.getAllStudent();
}
@Override
public Integer addStudent(Student student) {
return examDao.addStudent(student);
}
@Override
public List<Classes> getAllClasses() {
return examDao.getAllClasses();
}
}