MyBatis advanced
Today, I'd like to share with you the following
- Annotation implementation single table development
- Annotation for multi table operation
- SQL construction statement of MyBatis
Here are some key words of MyBatis annotation development
Annotation implementation single table development
The operation data in a table is relatively simple. Here, I will directly add, delete, modify and query together. The steps are as follows:
-
First, import the MyBatis related jar package (because JDBC related classes and log files are used, the corresponding package is imported)
-
Configure MyBatis core profile
<settings> <! -- display SQL statement in console -- > <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <mappers> <! -- load the package where the annotated interface is located -- > <package name="dao"/> </mappers>
- Add comments on the methods to be implemented in the interface and SQL statements to operate the database (this interface should be written under the package configured in the previous step)
public interface OneToOneMapper { @Select("select * from Card") List<Card> findCard(); }
- Write test class test results
Test class:
public class Test { private SqlSession sqlSession; private OneToOneMapper mapper; public Test() { this.sqlSession = SqlSessionUtils.getSession(); mapper = sqlSession.getMapper(OneToOneMapper.class); } @org.junit.Test public void test1() { List<Card> cards = mapper.findCard(); for (Card card : cards) { System.out.println(card.getId() + "," + card.getNumber()); } } }
Test results:
Addition, deletion and modification are similar to the above steps. I will not go over them here.
Annotation to realize multi table query
There are three situations to introduce here. The export package and configuration file are the same as above.
- MyBatis annotation for one-to-one query
First, add methods and comments in the CardMapper interface:
public interface CardMapper { @Select("select * from Card") @Results({ @Result(column = "id",property = "id"), @Result(column = "number",property = "number"), @Result( property = "p", javaType = Person.class, column = "pid", one = @One(select = "onetoone.PersonMapper.findById") ) }) List<Card> findAll(); }
Note: you should add an attribute corresponding to another entity class in the entity class corresponding to the interface.
Then define a method in another interface, PersonMapper, corresponding to the method in @ One in CardMapper,
The parameter to be passed in by this method is a column in the result of the SQL statement query in CardMapper.
public interface PersonMapper { @Select("select * from person where id=#{id}") Person findById(Integer id); }
Finally, write a test class to test the results:
public class Test01 { private SqlSession sqlSession; private OneToOneMapper mapper; private CardMapper cardMapper; public Test01() { this.sqlSession = SqlSessionUtils.getSession(); mapper = sqlSession.getMapper(OneToOneMapper.class); cardMapper = sqlSession.getMapper(CardMapper.class); } @Test public void test1() { List<Card> cards = mapper.findCard(); for (Card card : cards) { System.out.println(card); } } @Test public void test2() { List<Card> all = cardMapper.findAll(); for (Card card : all) { System.out.println(card); } } }
Query results:
2. MyBatis annotation implements one to many query
Define an interface ClassesMapper:
What's different here from one-to-one is that at the end of the annotation you use many = @Many (...)
public interface ClassesMapper { @Select("select * from classes where id=2") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result( column = "id", javaType = List.class, property = "students", many = @Many(select = "onetomany.StudentMapper.findStudentByCid") ) }) List<Classes> findClasses(); }
Corresponding interface:
public interface StudentMapper { @Select("select * from student where cid=#{cid}") List<Student> findStudentByCid(Integer cid); }
Test results:
public class Test01 { private static SqlSession sqlSession; private static StudentMapper studentMapper; private static ClassesMapper classesMapper; static { sqlSession = SqlSessionUtils.getSession(); classesMapper = sqlSession.getMapper(ClassesMapper.class); studentMapper = sqlSession.getMapper(StudentMapper.class); } @Test public void test1() { List<Classes> classes = classesMapper.findClasses(); for (Classes aClass : classes) { System.out.println(aClass.getId()+","+aClass.getName()); List<Student> students = aClass.getStudents(); for (Student student : students) { System.out.println("\t"+student); } } sqlSession.close(); } }
Test results:
3. MyBatis annotation realizes many to many query
Because many to many is two one to many, I won't go over it here.
SQL construction statement of MyBatis
Brief introduction:
This content is not commonly used, so I will demonstrate the format directly.
- First define a class to write out the SQL statement you want to implement
public class SqlDao { public String delete() { SQL sql = new SQL() { { DELETE_FROM("student"); WHERE("id=#{id}"); } }; return sql.toString(); } }
- Define another interface to call the SQL statement
public interface SqlMapper { @DeleteProvider(type = SqlDao.class, method = "delete") Integer delete(Integer id); }
- Write test classes to test results
public class Test { private SqlSession sqlSession; private SqlMapper sqlMapper; public Test() { this.sqlSession = SqlSessionUtils.getSession(); sqlMapper = sqlSession.getMapper(SqlMapper.class); } @org.junit.Test public void test1() { Integer delete = sqlMapper.delete(5); System.out.println(delete); } }
Test results:
The above is the introduction of this article. If there is something inappropriate, please correct it.