Spring + SpringMVC + mybatis + easyui integrated instance (2)

Source: Internet
Author: User

Spring + SpringMVC + mybatis + easyui integrated instance (2)

Directory:

Project Structure Description spring integrated mybatis and mybatis use test (annotation, non annotation) I. project structure:


ApplicationContext. xml is the spring configuration file. We put it under src to test mybatis.

Ii. Use mybatis

Let's use a simple example to illustrate how to integrate non-annotation form and annotation form mybatis and mybatis testing in spring.
First, import the spring core jar, mybatis jar, and spring to integrate mybatis jar.
Compile entity classes:

@Componentpublic class Student {        private int id;        private String name;        private String password;        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public String getPassword() {            return password;        }        public void setPassword(String password) {            this.password = password;        }}

The mybatis of the dao layer can be annotated or non-Annotated. Let's take a look at it one by one.

For non-annotation forms:
Compile StudentDaoIF. java in com. etoak. dao

@Repositorypublic interface StudentDaoIF {    public int addStudent(Student stu);    public int delStudentById(int id);    public int updateStudent(Student stu);    public Student selectStudentById(int id);    public List
  
    selectAllStudents();    public int StudentCount();    public List
   
     selectStudentByPage(Map map);} 
   
  

Add xml configuration under this package:
StudentDaoIF-mapper.xml

 
      
   
    
Insert into student values (null, # {name}, # {password });
       
   
    
Delete from student where id =#{ id };
       
   
    
Update student set name =#{ name}, password =#{ password} where id =#{ id };
       
       
   Select * from student where id =#{ id };    
       
   Select * from student;    
   Select count (*) from student;    
   Select * from student limit # {start}, # {max };       
       
           
            
             
       
      
     
    
   
  

Add:


      
       
        
            
             
                 
       
       
        classpath:com/etoak/dao/StudentDaoIF-mapper.xml
        
              
           
         
            
             
             
             
          
          
              
               
              
     
      
   
  

Compile the test class:

public class TestMybatis {    public static void main(String[] args) {         ApplicationContext ac = new ClassPathXmlApplicationContext(applicationContext.xml);          ac = new FileSystemXmlApplicationContext(src/applicationContext.xml);         StudentDaoIF dao = (StudentDaoIF)ac.getBean(studentDaoIF);         Student stu = (Student)ac.getBean(student);         stu.setName(a);         stu.setPassword(a1);         dao.addStudent(stu);    }}

For the annotation method mybatis:
Remove the StudentDaoIF-mapper.xml configuration, add annotations to the StudentDaoIF interface, and comment out the following lines in applicationContext. xml:


              
                   
    
     classpath:com/etoak/dao/StudentDaoIF-mapper.xml
                
           
  

Add the dao layer interface of the annotation:

@Repositorypublic interface StudentDaoIF {    @Insert(insert into student values(null,#{name},#{password}))    @Options(useGeneratedKeys = true , keyProperty = id)    public int addStudent(Student stu);    @Delete(delete from student where id=#{id})    public int delStudentById(int id);    @Update(update student set name=#{name},password=#{password} where id=#{id})    public int updateStudent(Student stu);    @Select(select * from student where id=#{id})    @Results({        @Result(column = id , property = id),        @Result(column = name , property = name),        @Result(column = password , property = password)    })    public Student selectStudentById(int id);    @Select(select * from student)    @Results({        @Result(column = id , property = id),        @Result(column = name , property = name),        @Result(column = password , property = password)    })    public List
  
    selectAllStudents();    @Select(select count(*) from student)    public int StudentCount();    @Select( select * from student limit #{start},#{max};)    @Results({        @Result(column = id , property = id),        @Result(column = name , property = name),        @Result(column = password , property = password)    })    public List
   
     selectStudentByPage(Map map);} 
   
  

We can also use the previous test class for testing.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.