The main points of

  • There is another way to accomplish statement mapping. The statements they map can be configured using Java annotations rather than XML.

  • Using annotations to map simple statements makes your code look cleaner, but for slightly more complex statements, Java annotations are not only ineffective, they can also clutter up your already complex SQL statements.

  • If you need to do something complicated, XML is the best way to map statements.

  • The Java interface needs to be registered in config.xml

<mappers>
        <mapper class="com.mybatis.DAO.PeopleMapper"/>
</mappers>
Copy the code

Class =

check

public interface PeopleMapper {
    @Select("select * from people")
    List<People> getPeopleList(a);
}
Copy the code

increase

You can start by enabling automatic transaction commit

public static SqlSession getSqlSession(a){
       return sqlSessionFactory.openSession(true); }}Copy the code

Mapper.java

public interface PeopleMapper {
    @Select("select * from people")
    List<People> getPeopleList(a);
    @Insert("insert into people(id, name, age, address) VALUES (#{id},#{name},#{age},#{address})")
    int addPeople(People people);
}
Copy the code

test

public class PeopleDAOtest {
    @Test
    public void print(a) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        List<People> people = peopleMapper.getPeopleList();
        for (People p :people){
            System.out.println(p);
        }
        sqlSession.close();
    }
    @Test
    public void add(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        peopleMapper.addPeople(new People(6."San Diego".456."What place?")); print(); }}Copy the code

SqlSession.com MIT () is not needed because it is already submitted automatically;

delete

  • Annotations @ Param
  1. Can only be used for basic data types
  2. The parameters passed in must be the same as those in the SQL statement

  1. Multiple parameters

  • The sample

Mapper.java

public interface PeopleMapper {
    @Delete("delete people from people where id=#{uid}")
    int delPeople(@Param("uid") int i);
}
Copy the code

test

public class PeopleDAOtest {
    @Test
    public void del(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        peopleMapper.delPeople(6); print(); }}Copy the code

change

Mapper.java

public interface PeopleMapper {
    @Update("update mybatis.people set name=#{name} ,age=#{age} ,address=#{address} where id=#{id}")
    int updateP(People people);
}

Copy the code

test

public class PeopleDAOtest {
    @Test
    public void update(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        peopleMapper.updateP(new People(5."San Diego".456."What place?")); print(); }}Copy the code