Spring Boot 会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory又会自动注入到Mapper中,所以这一切你都不用管了,直接拿起来使用就行了。 com.mysql.cj.jdbc.Driver是新版的驱动,老版本的是com.mysql.jdbc.Driver。
DO 就是一个普通的 JavaBean,Gender类型是我为了测试枚举类型的转换而引入的,你可以忽略或者直接基本类型来代替,没有影响。
运行结果:正常,会自动从 POJO 中抽取属性 生成语句:select id, user_name, gender from users where user_name=?
‘#{参数名}’ 【异常】
在#{}两边加上引号'
1 2
@Select("select " + cols + " from users where user_name='#{name}'") UserModel getOne(String name);
1 2 3
2018-04-20 17:11:45.606 DEBUG 51328 --- [ main] c.w.mybatis.mapper.UserMapper.getOne : ==> Preparing: select id, user_name, gender from users where user_name='?'
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', 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).
运行结果:异常 生成语句:select id, user_name, gender from users where user_name='?'
$ 号
${参数名} 【异常】
1 2
@Select("select " + cols + " from users where user_name=${name}") UserModel getOne(String name);
1
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String'
运行结果:异常 生成语句:无
${参数名}+@Param 【异常】
在方法参数前边加上@Param("name")
1 2
@Select("select " + cols + " from users where user_name=${name}") UserModel getOne(@Param("name") String name);
1 2 3 4 5 6 7 8
org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause' ### The error may exist in com/yibo/mybatis/mapper/UserMapper.java (best guess) ### The error may involve com.yibo.mybatis.mapper.UserMapper.getOne-Inline ### The error occurred while setting parameters ### SQL: select id, user_name, gender from users where user_name=Bob ### Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause' ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause'
运行结果:异常 生成语句:select id, user_name, gender from users where user_name=Bob
‘${参数名}‘+@Param 【正常】
在${}两边加上引号'并在方法参数前边加上@Param("name")
1 2
@Select("select " + cols + " from users where user_name='${name}'") UserModel getOne(@Param("name") String name);
运行结果:正常 生成语句:select id, user_name, gender from users where user_name='Bob'
${属性名} 【异常】
1 2
@Select("select " + cols + " from users where user_name=${userName}") UserModel getOne(UserModel user);
1 2 3 4 5 6 7 8
org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause' ### The error may exist in com/yibo/mybatis/mapper/UserMapper.java (best guess) ### The error may involve com.yibo.mybatis.mapper.UserMapper.getOne-Inline ### The error occurred while setting parameters ### SQL: select id, user_name, gender from users where user_name=Bob ### Cause: java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause' ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'Bob' in 'where clause'
运行结果:异常 生成语句:select id, user_name, gender from users where user_name=Bob
2018-04-20 17:45:25.375 DEBUG 51401 --- [ main] c.w.mybatis.mapper.UserMapper.getOne : ==> Preparing: select id, user_name, gender from users where user_name='?' 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).
运行结果:异常 生成语句:select id, user_name, gender from users where user_name='?'