spring boot用注解写sql

直接看例子

1.查

@Select("select * from users")
List<Users> AllUser();

2.改

//注意要加script标签
@Update("<script> " + "update users" +
            "<set>" + "<if test=\"name != null and name !=''\">name=#{name},</if>" +
            "<if test=\"nickname != null and nickname !=''\">nickname=#{nickname},</if>" +
            "<if test=\"url != null and url !=''\">url=#{url},</if>" +
            "<if test=\"sex != null and sex !=''\">sex=#{sex},</if>" +
            "<if test=\"age !=null and age !=''\">age=#{age},</if>" +
            "<if test=\"password !=null and password !=''\">password=#{password},</if>" +
            "</set>" + "where id=#{id}" +
            " </script> ")
int updateTest(Users users);

3.增

@Insert("insert into users (id,name,telephone,nickname,salt,userstate,password,jid,udate)\n" +
            "VALUES(#{id},#{name},#{telephone},#{nickname},#{salt},'01',#{password},'03',now())")
int in(Users users);

4.删

@Delete("delete from users where id = #{id}")
int delete(String id);

5.模糊查询(根据姓名和登记日期模糊查询所有数据)

 
    @Select({"select * from putong_rencai where concat(name,dengji_time) like '%' || 'search_name' || '%' "})
    public List<PuTongUser> selectMohu(@Param("search_name") String search_name);

个人觉得这个比mapper.xml要方便一些但是一些复杂的表关系还是不要用了,例如多对多一对多关系这种的

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
要创建一个Spring Boot MyBatis的SQL Server代码下载,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Java开发工具和Maven构建工具。 2. 创建一个新的Spring Boot项目。你可以通过使用Spring Initializr来快速搭建一个基本的Spring Boot项目结构。在Spring Initializr的网站上选择你需要的项目配置,包括项目的名称、包名、Java版本等。点击生成项目按钮,然后下载生成的zip包。 3. 解压下载的zip包,在你选择的目录下打开命令行或终端窗口。 4. 进入解压后的项目目录,运行以下Maven命令来导入项目依赖: ``` mvn clean install ``` 5. 接下来,你需要在项目的pom.xml文件中添加MyBatis和SQL Server的依赖。例如,你可以添加以下依赖: ``` <dependencies> ... <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>8.4.1.jre11</version> </dependency> ... </dependencies> ``` 6. 在项目的配置文件(例如application.properties或application.yml)中添加SQL Server的连接信息,包括数据库URL、用户名和密码。例如,你可以添加以下配置: ``` spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydatabase spring.datasource.username=yourusername spring.datasource.password=yourpassword ``` 7. 创建一个MyBatis的Mapper接口和对应的映射文件,用于定义和执行SQL查询语句。你可以在Mapper接口中声明SQL查询方,并且在映射文件中编对应的SQL语句。 8. 在你的应用程序中使用自动注入注解(例如@Autowired)将Mapper接口注入到你的服务类或控制器类中。然后,你就可以在这些类中使用Mapper接口定义的方来执行SQL查询了。 9. 最后,你可以启动你的Spring Boot应用程序,通过访问定义的接口来执行SQL查询操作。你可以使用Postman等工具来测试接口的响应结果。 希望以上步骤能够帮助你成功创建Spring Boot MyBatis的SQL Server代码,并实现相关的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Insist_on_progress

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值