Discuss / Java / 在AppConfig启动类写插入数据的代码,插入数据失败了

在AppConfig启动类写插入数据的代码,插入数据失败了

Topic source

AppConfig

public static void main(String[] args) throws Exception {
      Tomcat tomcat = new Tomcat();      
      tomcat.setPort(Integer.getInteger("port", 8080));      
      tomcat.getConnector();      
      Context ctx = tomcat.addWebapp("", new File("src/main/webapp").getAbsolutePath());      
      WebResourceRoot resources = new StandardRoot(ctx);
      resources.addPreResources(
            new DirResourceSet(resources, "/WEB-INF/classes", new File("target/classes").getAbsolutePath(), "/"));
      ctx.setResources(resources);
      tomcat.start();
      tomcat.getServer().await();
      //ApplicationContext context = new AnnotationConfigApplicationContext();      
      UserService userService = new UserService();       
      // 插入Alice:       
      if (userService.getUserByEmail("alice@example.com") == null) {
         userService.register("alice@example.com", "password2", "Alice");
      }
      // 插入Tom:
      if (userService.getUserByEmail("tom@example.com") == null) {
         userService.register("tom@example.com", "password3", "Tom");
      }
      // 插入Root:       
      try {
         userService.register("root@example.com", "password4", "root");
      } catch (RuntimeException e) {
         System.out.println(e.getMessage());       
      }
}

logger信息

2020-11-08 19:24:17 INFO  com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)
2020-11-08 19:24:17 INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2020-11-08 19:24:18 INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 1844 ms

浏览器访问只有一条数据,但这条数据是我访问controller创建的,并不是AppConfig插入的数据

[{"id":0,"email":"imaccep@gmail.com","name":"asd","createdAt":1604834309186,"imageUrl":"https://www.gravatar.com/avatar/533b3840351857d1a569cedec0a590d4","createdDateTime":"2020-11-08T19:18:29.186"}]

项目是直接下载老师的,依赖啥的都没修改。。

廖雪峰

#3 Created at ... [Delete] [Delete and Lock User]
tomcat.getServer().await();

会让main线程阻塞直到关闭tomcat,后面的代码才会执行

TEIAI_

#4 Created at ... [Delete] [Delete and Lock User]

在 DatabaseInitializer 最后添加

jdbcTemplate.update("INSERT INTO users(email,password,name,createdAt) VALUES('bob@example.com', 'bob123', 'Bob', 1630982262601)");
jdbcTemplate.update("INSERT INTO users(email,password,name,createdAt) VALUES('alice@example.com', 'helloalice', 'Alice', 1630982262645)");
jdbcTemplate.update("INSERT INTO users(email,password,name,createdAt) VALUES('tom@example.com', 'tomcat', 'Alice', 1630982262643)");

即可插入数据。

Ps.  HSQLDVB 的VALUES() 语句中必须使用 单引号 '' ,否则会报 user lacks privilege or object not found: 

全文代码:

public void init() {
	
	jdbcTemplate.update("drop table if exists users");
	
	jdbcTemplate.update("CREATE TABLE IF NOT EXISTS users (" //
			+ "id BIGINT IDENTITY NOT NULL PRIMARY KEY, " //
			+ "email VARCHAR(100) NOT NULL, " //
			+ "password VARCHAR(100) NOT NULL, " //
			+ "name VARCHAR(100) NOT NULL, " //
			+ "createdAt BIGINT NOT NULL, " //
			+ "UNIQUE (email))");

	jdbcTemplate.update(
			"INSERT INTO users(email,password,name,createdAt) VALUES('bob@example.com', 'bob123', 'Bob', 1630982262601)");
	jdbcTemplate.update(
			"INSERT INTO users(email,password,name,createdAt) VALUES('alice@example.com', 'helloalice', 'Alice', 1630982262645)");
	jdbcTemplate.update(
			"INSERT INTO users(email,password,name,createdAt) VALUES('tom@example.com', 'tomcat', 'Alice', 1630982262643)");
}

  • 1

Reply