我用springboot +的MyBatis並搭配SpringJUnit4ClassRunner運行,使用默認的數據庫H2和腳本:造成com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception未知列
創建表的SQL:
create TABLE IF NOT EXISTS `test_company` (
`id` bigint unsigned not null auto_increment,
`pid` bigint DEFAULT null COMMENT '',
`outer_id` bigint DEFAULT null COMMENT '',
`name` VARCHAR(256) DEFAULT NULL COMMENT '',
`parent_company_name` VARCHAR(256) DEFAULT NULL COMMENT '',
`user_id` bigint DEFAULT NULL COMMENT '用戶ID',
`user_name` VARCHAR(64) DEFAULT NULL COMMENT '',
`type` INTEGER DEFAULT null COMMENT '',
`level` INTEGER DEFAULT null COMMENT '',
`status` INTEGER DEFAULT null COMMENT ''
);
模型公司:
@Data
@NoArgsConstructor
public class Company implements Serializable {
private static final long serialVersionUID = -5822686079080905768L;
private Long id; private Long pid;
private Long outerId;
private String name;
private String parentCompanyName;
private Long userId;
private String userName;
private Integer type;
private Integer level;
private Integer status;
}
映射的結果映射:
<resultMap id="CompanyMap" type="Company">
<id column="id" property="id"/>
<result column="pid" property="pid"/>
<result column="outer_id" property="outerId"/>
<result column="name" property="name"/>
<result column="parent_company_name" property="parentCompanyName"/>
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="type" property="type"/>
<result column="level" property="level"/>
<result column="status" property="status"/>
</result>
個
一些sql語句:
<sql id="tb">
test_company
</sql>
<sql id="cols_all">
id, <include refid="cols_exclude_id" />
</sql>
<sql id="cols_exclude_id">
pid, outer_id, `name`, `parent_company_name`, user_id, user_name, `type`, `level`, status
</sql>
<sql id="vals">
#{pid}, #{outerId}, #{name}, #{parentCompanyName}, #{userId}, #{userName}, #{type}, #{level}, #{status}
</sql>
創建SQL:
<insert id="create" parameterType="Company" keyProperty="id" useGeneratedKeys="true">
INSERT INTO
<include refid="tb" />
(<include refid="cols_exclude_id" />)
VALUES
(<include refid="vals" />)
</insert>
運行狀況良好之前沒有列parent_company_name
,
,但是當我加列parent_company_name
並重新運行DAO方法創建它有這個錯誤
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'parent_company_name' in 'field list'
### The error may involve Company.create-Inline
### The error occurred while setting parameters
我的IDE是IntelliJ IDEA,我不知道是否由緩存或別的東西引起,我只是添加了一個新列,並檢查了所有的SQL腳本是正確的。
謝謝,通過h2測試,它會在我本地的mysql服務器上創建一個表,並且應該添加你的sql'alert table ...'來更新數據庫。 –
@BenD如果這個答案適合你。請標記爲正確的答案。 – Sohlowmawn