mybatis使用注解开发
阅读该篇文章将默认您已经能够熟练使用mybatis配置mapper.xml进行开发了。首先把pom.xml贴出来吧。里面有注释

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zdsoft</groupId> <artifactId>mybatis-test</artifactId> <version>1.0.0</version> <dependencies> <!-- mybatis库 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <!-- json序列化 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!-- 日志输出 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!-- 数据库访问,mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <build> <finalName>mybatis_test</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Java代码mapper如下:
package zdsoft.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.SelectKey; import org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.jdbc.SQL; import zdsoft.entity.Student; public interface StudentMapper { @Insert("insert into student(id,name,age) values(#{id},#{name},#{age})") @SelectKey(keyProperty = "id", before = true, resultType = int.class, statement = "SELECT IFNULL(MAX(id),0)+1 FROM student") public void insert(Student stu); @Select("select * from student") public List<Student> findAll(); @Delete("delete from student where id=#{id}") public int delete(int id); @SelectProvider(method = "find", type = StudentBuilder.class) public List<Student> find(@Param("name") String name, @Param("age") int age); class StudentBuilder { public String find(@Param("name") final String name, @Param("age") final int age) { return new SQL() { { SELECT("*"); FROM("student"); if (name != null) { WHERE("NAME LIKE CONCAT(#{name},'%')"); } if (age > 0) { WHERE("age=#{age}"); } ORDER_BY("id desc"); } }.toString(); } } }
其它的就不用多说了,和配置xml一样的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
2016-03-29 生成全球唯一标识GUID