MyBatis插入数据

使用${}插入数据成功,但是使用#{}失败,想请教一下为什么使用#{}会失败。

Emp类如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Short gender;
    private String image;
    private Short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

测试类如下:

//新增员工
    @Test
    public void testInsert(){
        //构造员工对象
        Emp emp = new Emp();
        emp.setUsername("fanyao");
        emp.setName("范遥");
        emp.setImage("1.jpg");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntrydate(LocalDate.of(2016,1,1));
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(1);

        //执行新增员工信息操作
        empMapper.insert(emp);
        System.out.println(emp.getId());
    }

使用#{}插入数据

@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            "VALUES ('#{username}', '#{name}', '#{gender}', '#{image}', '#{job}', '#{entrydate}', '#{deptId}', '#{createTime}', '#{updateTime}')")
    public void insert(Emp emp);

运行结果如下:
主要报错提示:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='username', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).


使用${}插入数据:

    @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            "VALUES ('${username}', '${name}', '${gender}', '${image}', '${job}', '${entrydate}', '${deptId}', '${createTime}', '${updateTime}')")
    public void insert(Emp emp);

运行结果:

D:\dev\wcjdk11\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=3161:D:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\wc189\.m2\repository\org\junit\platform\junit-platform-launcher\1.8.2\junit-platform-launcher-1.8.2.jar;C:\Users\wc189\.m2\repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;C:\Users\wc189\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\wc189\.m2\repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;C:\Users\wc189\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\plugins\junit\lib\junit-rt.jar;D:\codes\JavaWeb\itheima_JavaWeb\springboot-mybatis-crud\target\test-classes;D:\codes\JavaWeb\itheima_JavaWeb\springboot-mybatis-crud\target\classes;D:\dev\apache-maven-3.6.1\mvn_repo\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.2.2\mybatis-spring-boot-starter-2.2.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-starter\2.7.5\spring-boot-starter-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot\2.7.5\spring-boot-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-autoconfigure\2.7.5\spring-boot-autoconfigure-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-starter-logging\2.7.5\spring-boot-starter-logging-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\dev\apache-maven-3.6.1\mvn_repo\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\dev\apache-maven-3.6.1\mvn_repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-starter-jdbc\2.7.5\spring-boot-starter-jdbc-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-jdbc\5.3.23\spring-jdbc-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-tx\5.3.23\spring-tx-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.2.2\mybatis-spring-boot-autoconfigure-2.2.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\mybatis\mybatis-spring\2.0.7\mybatis-spring-2.0.7.jar;D:\dev\apache-maven-3.6.1\mvn_repo\com\mysql\mysql-connector-j\8.0.31\mysql-connector-j-8.0.31.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\projectlombok\lombok\1.18.24\lombok-1.18.24.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-starter-test\2.7.5\spring-boot-starter-test-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-test\2.7.5\spring-boot-test-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\boot\spring-boot-test-autoconfigure\2.7.5\spring-boot-test-autoconfigure-2.7.5.jar;D:\dev\apache-maven-3.6.1\mvn_repo\com\jayway\jsonpath\json-path\2.7.0\json-path-2.7.0.jar;D:\dev\apache-maven-3.6.1\mvn_repo\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;D:\dev\apache-maven-3.6.1\mvn_repo\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\ow2\asm\asm\9.1\asm-9.1.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\dev\apache-maven-3.6.1\mvn_repo\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;D:\dev\apache-maven-3.6.1\mvn_repo\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\assertj\assertj-core\3.22.0\assertj-core-3.22.0.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\mockito\mockito-core\4.5.1\mockito-core-4.5.1.jar;D:\dev\apache-maven-3.6.1\mvn_repo\net\bytebuddy\byte-buddy\1.12.18\byte-buddy-1.12.18.jar;D:\dev\apache-maven-3.6.1\mvn_repo\net\bytebuddy\byte-buddy-agent\1.12.18\byte-buddy-agent-1.12.18.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\objenesis\objenesis\3.2\objenesis-3.2.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\mockito\mockito-junit-jupiter\4.5.1\mockito-junit-jupiter-4.5.1.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;D:\dev\apache-maven-3.6.1\mvn_repo\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\springframework\spring-test\5.3.23\spring-test-5.3.23.jar;D:\dev\apache-maven-3.6.1\mvn_repo\org\xmlunit\xmlunit-core\2.9.0\xmlunit-core-2.9.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.wangchen.SpringbootMybatisCrudApplicationTests,testInsert
17:22:49.914 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
17:22:49.924 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
17:22:49.959 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.wangchen.SpringbootMybatisCrudApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
17:22:49.971 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.wangchen.SpringbootMybatisCrudApplicationTests], using SpringBootContextLoader
17:22:49.975 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.wangchen.SpringbootMybatisCrudApplicationTests]: class path resource [com/wangchen/SpringbootMybatisCrudApplicationTests-context.xml] does not exist
17:22:49.975 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.wangchen.SpringbootMybatisCrudApplicationTests]: class path resource [com/wangchen/SpringbootMybatisCrudApplicationTestsContext.groovy] does not exist
17:22:49.975 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.wangchen.SpringbootMybatisCrudApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
17:22:49.976 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.wangchen.SpringbootMybatisCrudApplicationTests]: SpringbootMybatisCrudApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
17:22:50.016 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.wangchen.SpringbootMybatisCrudApplicationTests]
17:22:50.088 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\codes\JavaWeb\itheima_JavaWeb\springboot-mybatis-crud\target\classes\com\wangchen\SpringbootMybatisCrudApplication.class]
17:22:50.089 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.wangchen.SpringbootMybatisCrudApplication for test class com.wangchen.SpringbootMybatisCrudApplicationTests
17:22:50.181 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.wangchen.SpringbootMybatisCrudApplicationTests]: using defaults.
17:22:50.182 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
17:22:50.191 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [javax/servlet/ServletContext]
17:22:50.201 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4b45dcb8, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@7216fb24, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@2072acb2, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@50ecde95, org.springframework.test.context.support.DirtiesContextTestExecutionListener@35a9782c, org.springframework.test.context.transaction.TransactionalTestExecutionListener@70a36a66, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@45815ffc, org.springframework.test.context.event.EventPublishingTestExecutionListener@732f29af, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@d3957fe, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@6622fc65, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@299321e2, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@23fb172e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@64ba3208, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@158a3b2e]
17:22:50.206 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@56303b57 testClass = SpringbootMybatisCrudApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@4b2a01d4 testClass = SpringbootMybatisCrudApplicationTests, locations = '{}', classes = '{class com.wangchen.SpringbootMybatisCrudApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@9816741, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2f67a4d3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5c86dbc5, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@24105dc5, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@18317edc, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5f71c76a], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
17:22:50.221 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@56303b57 testClass = SpringbootMybatisCrudApplicationTests, testInstance = com.wangchen.SpringbootMybatisCrudApplicationTests@61ce23ac, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@4b2a01d4 testClass = SpringbootMybatisCrudApplicationTests, locations = '{}', classes = '{class com.wangchen.SpringbootMybatisCrudApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@9816741, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2f67a4d3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5c86dbc5, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@24105dc5, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@18317edc, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5f71c76a], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]].

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.5)

2023-08-24 17:22:50.533  INFO 45524 --- [           main] .w.SpringbootMybatisCrudApplicationTests : Starting SpringbootMybatisCrudApplicationTests using Java 11.0.15.1 on wangchen with PID 45524 (started by wc189 in D:\codes\JavaWeb\itheima_JavaWeb\springboot-mybatis-crud)
2023-08-24 17:22:50.534  INFO 45524 --- [           main] .w.SpringbootMybatisCrudApplicationTests : No active profile set, falling back to 1 default profile: "default"
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Property 'mapperLocations' was not specified.
2023-08-24 17:22:51.451  INFO 45524 --- [           main] .w.SpringbootMybatisCrudApplicationTests : Started SpringbootMybatisCrudApplicationTests in 1.207 seconds (JVM running for 2.161)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@39666e42] was not registered for synchronization because synchronization is not active
2023-08-24 17:22:51.788  INFO 45524 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-08-24 17:22:52.248  INFO 45524 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@366098173 wrapping com.mysql.cj.jdbc.ConnectionImpl@6b9c42bd] will not be managed by Spring
==>  Preparing: insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)VALUES ('fanyao', '范遥', '1', '1.jpg', '1', '2016-01-01', '1', '2023-08-24T17:22:51.731918700', '2023-08-24T17:22:51.731918700')
==> Parameters: 
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@39666e42]
null
2023-08-24 17:22:52.307  INFO 45524 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-08-24 17:22:52.313  INFO 45524 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

进程已结束,退出代码0


去掉值前后的单引号

【以下回答由 GPT 生成】

我了解并能够回答这个问题。

对于使用${}和#{}插入数据的区别,我没有疑问。

${}语法是用于在字符串中插入变量的值,它主要用于动态替换字符串中的值。在${}中可以使用任何有效的表达式,并且会将表达式的结果转换为字符串。下面是一个示例代码:

String name = "John";
String message = "Hello, ${name}!"; // 使用${}插入变量的值
System.out.println(message);

输出结果为:"Hello, John!"

{}语法是用于在SQL语句中插入参数的值,主要用于预处理SQL语句,防止SQL注入攻击。在#{}中只能插入参数,不能插入表名、列名等。下面是一个示例代码:

String name = "John";
String sql = "SELECT * FROM users WHERE name = #{name}"; // 使用#{}插入参数的值
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
ResultSet resultSet = statement.executeQuery();

在上面的代码中,#{}确保了name的值被安全地插入到SQL语句中,并且PreparedStatement类会自动处理参数的转义和占位符。

总结起来,${}用于在字符串中插入变量的值,#{}用于在SQL语句中插入参数的值。它们在使用上有一些区别,并且各自有不同的用途。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^