1. 导入jar包
导入mybatis所有jar和spring基本包;
spring-jdbc spring-tx(事务) spring-aop(面向切面) spring-web(配置监听器Listener) spring整合mybatis的包等
2. 先配置web.xml
web.xml的配置中<context-param>配置作用
|
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 加载Spring的配置文件 --> <!-- 上下文参数 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- spring配置文件 --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 封装了一个监听器listener,帮助加载Spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
3.编写spring配置文件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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 数据源封装类 .数据源:获取数据库连接,spring-jdbc.jar中--> <bean id="dataScource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/ssm"></property> <property name="username" value="root"></property> <property name="password" value="lc817"></property> </bean> <!-- 创建SqlSessionFactory对象 --> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接信息来源于dataSource --> <property name="dataSource" ref="dataScource"></property> </bean> <!-- 扫描包:扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 要扫描哪个包 --> <property name="basePackage" value="com.mzx.mapper"></property> <!-- 和factory产生关系 --> <property name="sqlSessionFactory" ref="factory"></property> </bean> <!-- 由spring管理service实现类,配置ServiceImp --> <bean id="studentServiceImp" class="com.mzx.serviceImp.StudentServiceImp"> <property name="sm" ref="studentMapper"></property> </bean> </beans> |
4.编写代码
1. 正常编写pojo
2. 编写mapper包下时,必须使用接口绑定方案或注解方案(必须有接口)
3. 正常编写Service接口和Service实现类
3.3.1 需要在Service实现类中声明Mapper接口对象,并生成get/set方法
public interface StudentService {
List<Student> getAllStudentInfo();
}
public class StudentServiceImp implements StudentService {
private StudentMapper sm;
public StudentMapper getSm() {
return sm;
}
public void setSm(StudentMapper sm) {
this.sm = sm;
}
@Override
public List<Student> getAllStudentInfo() {
return sm.selStu();
}
4. spring无法管理Servlet,在service中取出Service对象
木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。
@WebServlet("/show")
public class StudentServlet extends HttpServlet{
private StudentService ss;
@Override
public void init() throws ServletException {
/* //获取Spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取响应对象
ss=(StudentService) ac.getBean("serviceImp"); */
//spring和web整合后所有信息都存放在webApplicationContext
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ss=(StudentService) ac.getBean("serviceImp");}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求编码格式
//设置响应编码格式
//获取请求信息
//处理请求信息
List<Student> list=ss.getAllStudentInfo();
//响应处理结果
System.out.println(list);
}}