Код: Выделить всё
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
Код: Выделить всё
jdbcTemplate.execute("DROP TABLE IF EXISTS user");
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS user (\n" +
" id INT AUTO_INCREMENT PRIMARY KEY,\n" +
" name VARCHAR(250) NOT NULL\n" +
")");
Код: Выделить всё
KeyHolder keyHolder = new GeneratedKeyHolder();
String query = "insert into user (name) values ('Foo')";
// create the mysql insert preparedstatement
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(query, new String[]{"PrimaryKeyColumnName"});
return ps;
}, keyHolder);
for (Map.Entry entry : keyHolder.getKeys().entrySet()) {
System.out.println("ENTRY: " + entry);
List result = jdbcTemplate.queryForList("SELECT * FROM user WHERE " + entry.getKey() + "= ?", entry.getValue());
System.out.println("result: " + result);
}
Код: Выделить всё
# MySQL Config
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57InnoDBDialect
Код: Выделить всё
ENTRY: GENERATED_KEY=1
PreparedStatementCallback; bad SQL grammar [SELECT * FROM user WHERE GENERATED_KEY= ?]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'GENERATED_KEY' in 'where clause'
Код: Выделить всё
# H2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
Код: Выделить всё
ENTRY: ID=1
Любые входные данные здесь очень ценятся!
Подробнее здесь: https://stackoverflow.com/questions/677 ... y-on-mysql
Мобильная версия