SM整合-方式二

package org.lanqiao.entity;

public class Student {
	private int stuNo;
	private String stuName;
	private int stuAge;
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	
}


	
	
		
			
				classpath:db.properties
			
		
	
	
	
	
	 
	
		
		
		 
	
	
	
		
	
	
	
	
		
		
		
		 
	
	
	
	
		
		
		
	
	

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=lmt568899
maxActive=500
maxIdle=1000
package org.lanqiao.mapper;

import org.lanqiao.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student);
}



	
	
		insert into student(stuNo,stuName,stuAge)
		values(#{stuNo},#{stuName},#{stuAge})
	
package org.lanqiao.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{
	@Override
	public void addStudent(Student student){
		SqlSession session = super.getSqlSession();
		// 和 conf.xml一样
		StudentMapper stuDao = session.getMapper(StudentMapper.class);
		stuDao.addStudent(student);
	}
}
package org.lanqiao.service;
 
import org.lanqiao.entity.Student; 

public interface IStudentService {
	public void addStudent(Student student);
}
package org.lanqiao.service.impl;

import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private StudentMapper studentMapper;
	
	public void setStudentMapper(StudentMapper studentMapper){
		this.studentMapper = studentMapper;
	}
	
	@Override
	public void addStudent(Student student){
		//   调用 dao 层
		studentMapper.addStudent(student);
	}
}
package org.lanqiao.test;

import org.lanqiao.entity.Student;
import org.lanqiao.service.IStudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		IStudentService studentService =(IStudentService)context.getBean("studentService");
		Student student = new Student();
		student.setStuAge(88);
		student.setStuName("zds");
		student.setStuNo(12);
		studentService.addStudent(student);
	}
}

SM整合-方式二_第1张图片

你可能感兴趣的:(SSM)