栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于xml文件Spring整合Mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于xml文件Spring整合Mybatis

1.引入依赖


    junit
    junit
    4.11



    org.mybatis
    mybatis-spring
    2.0.3



    org.mybatis
    mybatis
    3.5.1



    com.alibaba
    druid
    1.1.22


    org.springframework
    spring-context
    5.0.5.RELEASE


    org.springframework
    spring-beans
    5.0.5.RELEASE



    org.springframework
    spring-jdbc
    5.3.5




    org.aspectj
    aspectjweaver
    1.9.6
    runtime


    mysql
    mysql-connector-java
    5.1.46




    
        
            src/main/java
            
                ***.xml
            
            false
        
    

2.编写entity实体类Student
  • public class Student {
        private Integer Sid;
        private String Sname;
        private String Sage;
        private String Ssex;
    	
        getter和setter方法
    }
    
3.编写Dao层
  • 创建StudentDao

    • public interface StudentDao {
          List getAllStudent();
      }
      
  • 编写StudentDao.xml配置文件

    • 
      
      
          
              select * from student
          
      
      
4.编写Service层
  • 创建StudentService

    • public interface StudentService {
      List getAllStudent();
      }
      
  • 创建StudentServiceImpl实现StudentService

    • public class StudentServiceImpl implements StudentService {
          public StudentDao getStudentDao() {
              return studentDao;
          }
      
          public void setStudentDao(StudentDao studentDao) {
              this.studentDao = studentDao;
          }
      
          private StudentDao studentDao;
          public List getAllStudent() {
              return studentDao.getAllStudent();
          }
      }
      
5.创建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/practice?userUnicode=True&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
6.创建mybatis-config.xml配置文件



    
    
        
    
    
    
        
    

7.创建applicationContext.xml


    
    
    
    
        
        
        
        
    
    
    
        
        
        
    
    
    
        
    
    
    
        
        
    
    
    
        
    

8.编写测试类
public class StudentServiceImplTest {
    @Test
    public void getAllStudent() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = (StudentService) context.getBean("studentService");
        List allStudent = studentService.getAllStudent();
        for (Student student : allStudent) {
            System.out.println(student.getSname());
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号