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.