Mybatis注解增刪改查的實例代碼
要點
有另一種方法來完成語句映射。 它們映射的語句可以不用 XML 來配置,而可以使用 Java 注解來配置。
使用注解來映射簡單語句會使代碼顯得更加簡潔,但對于稍微復雜一點的語句,Java 注解不僅力不從心,還會讓你本就復雜的 SQL 語句更加混亂不堪。
如果你需要做一些很復雜的操作,最好用 XML 來映射語句。
需要在config.xml中注冊Java接口
<mappers> <mapper class="com.mybatis.DAO.PeopleMapper"/> </mappers>
要用class=
查
public interface PeopleMapper { @Select("select * from people") List<People> getPeopleList(); }
增
可以先開啟事務自動提交
public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(true); } }
Mapper.java
public interface PeopleMapper { @Select("select * from people") List<People> getPeopleList(); @Insert("insert into people(id, name, age, address) VALUES (#{id},#{name},#{age},#{address})") int addPeople(People people); }
test
public class PeopleDAOtest { @Test public void print() { 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(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class); peopleMapper.addPeople(new People(6,"圣迭戈",456,"啥地方")); print(); } }
因為已經(jīng)自動提交了,所以不需要sqlSession.commit();
刪
注解@Param
只能用于基本數(shù)據(jù)類型
傳入的參數(shù)只能和sql語句中參數(shù)一樣
多個參數(shù)
樣例
public interface PeopleMapper { @Delete("delete people from people where id=#{uid}") int delPeople(@Param("uid") int i); }
test
public class PeopleDAOtest { @Test public void del(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class); peopleMapper.delPeople(6); print(); } }
改
Mapper.java
public interface PeopleMapper { @Update("update mybatis.people set name=#{name} ,age=#{age} ,address=#{address} where id=#{id}") int updateP(People people); }
test
public class PeopleDAOtest { @Test public void update(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class); peopleMapper.updateP(new People(5,"圣迭戈",456,"啥地方")); print(); } }
總結(jié)
到此這篇關(guān)于Mybatis注解增刪改查的文章就介紹到這了,更多相關(guān)Mybatis注解增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Oracle + mybatis實現(xiàn)對數(shù)據(jù)的簡單增刪改查實例代碼
- Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(二)
- Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(一)
- Mybatis實現(xiàn)數(shù)據(jù)的增刪改查實例(CRUD)
- 簡述Mybatis增刪改查實例代碼
- Mybatis實現(xiàn)增刪改查(CRUD)實例代碼
- SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能
- Mybatis實現(xiàn)增刪改查
- Mybatis步驟分解實現(xiàn)一個增刪改查程序
相關(guān)文章
@RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)
這篇文章主要介紹了@RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot + validation 接口參數(shù)校驗的思路詳解
這篇文章主要介紹了SpringBoot + validation 接口參數(shù)校驗,本文通過項目實踐+場景分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10SpringMVC 重定向參數(shù)RedirectAttributes實例
這篇文章主要介紹了SpringMVC 重定向參數(shù)RedirectAttributes實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12