标签: mybatis

java 和 mybatis - 误解一对多关系 - 仅注释

我有两个表:人和狗。您知道这个人可能拥有不止一只狗。
我的模型是:

public class {
  int personId;
  List <Dog> dogs;
  String name;
  String surname;
}
public class Dog{
   String name;
   int dogId;
}
Run Code Online (Sandbox Code Playgroud)

当谈到数据库时,它非常相似:

PersonID(PK), Name (String), surname(String)   
dogId(PK), name(String), IdOwner(FK)   
Run Code Online (Sandbox Code Playgroud)

你能帮我在mybatis中写select吗?我尝试阅读有关@one和 的内容@many

java mybatis spring-mybatis

2
推荐指数
1
解决办法
6303
查看次数

如何在 Spring Boot application.properties 文件中设置 mybatis 属性?

我正在开发一个使用 mybatis 的 spring boot 应用程序。我的 mybatis mapper xml 有一个 SQL 查询,其中包含需要从应用程序属性设置的数据库模式名称。我有一个架构名称,我想在我的mapper.xml${schema-name} 中引用它。我可以这样做mybatis-config.xml- 像这样:

<configuration>    
   <PropertiesSource url='my properties file"/>   
   <properties>
     <property name='schema-name' value='${database.schema}'/> 
   </properties>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但我不想使用mybatis-config.xml. 我想使用spring bootapplication.properties进行mybatis配置。

有没有办法在spring boot application.properties中设置mybatis配置属性?有人可以帮我弄这个吗?

spring mybatis spring-boot spring-mybatis

2
推荐指数
1
解决办法
5838
查看次数

使用 MyBatis Annotation Spring Boot 检索生成的 ID

我正在努力学习MyBatis。使用注释插入语句后如何获取自动生成的 ID @Insert

我的代码示例:

@Insert("INSERT INTO user(name, mobile, password) VALUES(#{name}, #{mobile}, #{password})")
@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
Long insertUser(User user);
Run Code Online (Sandbox Code Playgroud)

我想获取生成的 id 作为插入方法的返回。

intellij-idea mybatis spring-boot spring-mybatis

2
推荐指数
1
解决办法
3780
查看次数

如何在 ResponseEntity 中不返回空值?

我用来ResponseEntity返回 GET“api/v1/name”和 POST“api/v1/name”请求的响应。

我的目标不是返回空值响应,例如在 POST“api/v1/name”请求中,当前响应正文为:

{
    "id": null,
    "name": "who",
    "newid": "A8C90A53-78F6-4BD6-9628-CBA8FC349C08"
}
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像:

{
    "name": "who",
    "newid": "A8C90A53-78F6-4BD6-9628-CBA8FC349C08"
}
Run Code Online (Sandbox Code Playgroud)

在我看来,使用下面的代码重新创建对象只会使代码的可读性降低,并且可能会使用更多内存(我不确定,如果我错了,请告诉我):

...
Map<String, String> responseBody = new HashMap<>();
responseBody.put("name", nameModel.getName());
responseBody.put("newid", nameModel.getNewId());

return new ResponseEntity<>(responseBody, HttpStatus.OK);
Run Code Online (Sandbox Code Playgroud)

==== 下面是完整的存储库,如果您想查看更新的存储库:https ://github.com/kidfrom/g2_java/tree/main/etc/mssqlserver

控制器/NameController.java

package com.example.mssqlserver.controller;

import com.example.mssqlserver.mapper.NameMapper;
import com.example.mssqlserver.model.NameModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class NameController {

  @Autowired
  private NameMapper nameMapper;

  @GetMapping("api/v1/name")
  public ResponseEntity<?> selectAll() {
    return new ResponseEntity<>(nameMapper.selectAll(), HttpStatus.OK);
  }

  @PostMapping("api/v1/name")
  public ResponseEntity<?> insert(@RequestBody …
Run Code Online (Sandbox Code Playgroud)

java spring mybatis spring-boot

2
推荐指数
1
解决办法
2万
查看次数

基于XML +注释的MyBatis配置

是否可以在应用程序中同时具有基于XML + Annotation的MyBatis配置.

我之所以这样问是因为,在我的应用程序中,我使用的是基于注释的方法.但在其中一个场景中,我需要使用一个IN子句,它可以使用

<foreach></foreach>
Run Code Online (Sandbox Code Playgroud)

基于XML的配置.

但是,当我启动我的应用程序时,它似乎无法识别我的基于注释的映射器,并给了我一个Type interface is not known to the MapperRegistry例外

因此,我想知道是否可以在应用程序中同时使用基于XML + Annotation的MyBatis配置.请建议.

java mybatis

1
推荐指数
1
解决办法
5804
查看次数

MyBatis:ClassCastException:java.lang.Integer无法强制转换为java.lang.Boolean

我在尝试执行更新语句(UpdateUser)时在MyBatis中收到此异常:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean
at $Proxy6.updateUser(Unknown Source)
at com.nortal.pirs.persistence.dbmybatis.UserDaoMyBatisImpl.updateUser(UserDaoMyBatisImpl.java:60)
at com.nortal.pirs.test.persistence.UserDaoAbstractTest.testUpdateUser(UserDaoAbstractTest.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)

我的数据类型我正在使用的用户看起来像这样:

public class User {
private String firstName = "";
private …
Run Code Online (Sandbox Code Playgroud)

java boolean classcastexception mybatis

1
推荐指数
1
解决办法
1万
查看次数

有条件地在MyBatis中执行<selectKey>

我有这个问题.只有当提供的bookTypeId为null时,我才需要执行selectKey来为我提供id .可以吗?

这是插入函数映射:

<insert id="insert" parameterType="BookType">
    <selectKey resultType="Integer" keyProperty="bookTypeId" order="BEFORE">
        SELECT nextval('seq_book_type')
    </selectKey>
    INSERT INTO book_type(
        book_type_id,
        book_type_desc
    )
    VALUES(
        #{bookTypeId},
        #{bookTypeDesc}
    )
</insert>
Run Code Online (Sandbox Code Playgroud)

mybatis

1
推荐指数
1
解决办法
4748
查看次数

MyBatis if子句

我在MyBatis中尝试了以下if子句,但出现以下异常,请在这里帮助我确定问题。

public class Student{

private Integer studId;
private String name;
private String email;
private Date dob;
}
Run Code Online (Sandbox Code Playgroud)

制图

<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult">
    <![CDATA[
    SELECT * FROM STUDENTS
    WHERE 1 = 1 

    <if test="studId != null">
    AND STUD_ID= #{studId}
    </if>

    <if test="name != null">
    AND NAME like #{name}
    </if>

    ]]>
</select>
Run Code Online (Sandbox Code Playgroud)

我得到的异常:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL   syntax; check the manual that corresponds to your MySQL server version …
Run Code Online (Sandbox Code Playgroud)

mybatis

1
推荐指数
1
解决办法
8910
查看次数

Too many connections with Spring + MyBatis + MySQL + JDBC

I am trying to work with spring, mybatis and mysql with jdbc. The only small problem I have after some queries, it throws an exception. The DB says: Too many connections.

So what I am trying to do to prevent the too many connection issue is to set up a connection pool. I have tried dbcp and bonecp also but both have the same behaviour. When I reload my page it just keeps loading in the browser and after some …

java spring connection-pooling jdbc mybatis

1
推荐指数
1
解决办法
2万
查看次数

Mybatis结果声明重用

下面的代码是正常工作的mapper类,但是它具有结果集声明的复制内容(在我的情况下这是很大的)。
如何重用@Results声明?

@Mapper
public interface DailyMasterCurrentTradeDao {
    @Select("select * from dly_mstr_curr_trd")
    @Results({
        @Result(property = "accAdsStC", column = "ACC_ADS_ST_C"),
        ...
    })
    List<DailyMasterCurrentTrade> selectDailyMasterCurrentTrades();

    @Select("select * from dly_mstr_curr_trd where rownum < #{rownumThreshold}")
    @Results({
        @Result(property = "accAdsStC", column = "ACC_ADS_ST_C"),
        ...
    })
    List<DailyMasterCurrentTrade> selectFewDailyMasterCurrentTrades(long rownumThreshold);
}
Run Code Online (Sandbox Code Playgroud)

java annotations mybatis

1
推荐指数
1
解决办法
2163
查看次数