質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

10401閲覧

Spring Boot MavenでSQLite3に接続したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2016/12/22 06:50

編集2016/12/22 06:52

Spring Boot MavenでSQLite3に接続したいのですが、
STSを使用して「Spring Boot アプリケーション」を起動すると、
以下のエラーが出て起動出来ません。

(文字制限内とする為、import文などを削除してあります。)

「...Cannot load driver class: "org.sqlite.JDBC"」
###発生している問題・エラーメッセージ

2016-12-22 14:55:50.236 WARN 7824 --- [main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: "org.sqlite.JDBC" ...

###該当のソースコード
SQLiteOperatorApplication.java

@SpringBootApplication public class SQLiteOperatorApplication { public static void main(String[] args) { SpringApplication.run(SQLiteOperatorApplication.class, args); } }

ServletInitializer.java

public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SQLiteOperatorApplication.class); } }

Operator.java

@Entity @Table(name="operator") public class Operator { @Id @Column @NotNull @NotEmpty @Getter @Setter private int operatorId; @Column @NotNull @NotEmpty @Getter @Setter private String employmentId; @Column @NotNull @NotEmpty @Getter @Setter private String password; @Column @NotNull @NotEmpty @Getter @Setter private String userName; @Column @NotNull @NotEmpty @Getter @Setter private int userAuthority; }

OperatorController.java

@Controller public class OperatorController { protected static Logger log = LoggerFactory.getLogger( OperatorController.class ); @Autowired OperatorRepository repository; @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView index( @ModelAttribute("formModel")Operator operator, ModelAndView mav) { log.info("★★★root accessed.★★★"); mav.setViewName("index"); mav.addObject("msg","this is operator list."); mav.addObject("formModel",operator); Iterable<Operator> list = repository.findAll(); mav.addObject("operatorlist",list); return mav; } @RequestMapping(value = "/", method = RequestMethod.POST) @Transactional(readOnly=false) public ModelAndView form( @ModelAttribute("formModel") @Validated Operator operator, BindingResult result, ModelAndView mov) { ModelAndView res = null; if (!result.hasErrors()){ repository.saveAndFlush(operator); res = new ModelAndView("redirect:/"); } else { mov.setViewName("index"); mov.addObject("msg","sorry, error is occured..."); Iterable<Operator> list = repository.findAll(); mov.addObject("datalist",list); res = mov; } return res; } }

OperatorRepository.java

@Repository public interface OperatorRepository extends JpaRepository<Operator, Long> { public Operator findByOperatorId(int operatorId); public List<Operator> findByEmploymentId(String employmentId); public List<Operator> findByPasswordLike(String password); public List<Operator> findByUserNameLike(String userName); }

index.html

<body> <h1 th:text="${msg}"></h1> <table border="2"> <tr><th>ID</th><th>職番</th><th>パスワード</th><th>氏名</th><th>権限</th></tr> <tr th:each="obj : ${operatorlist}"> <td th:text="${obj.operatorId}"></td> <td th:text="${obj.employmentId}"></td> <td th:text="${obj.password}"></td> <td th:text="${obj.userName}"></td> <td th:text="${obj.userAuthority}"></td> </tr> </table> </body> </html>

application.properties

spring.datasource.driverClassName="org.sqlite.JDBC" spring.datasource.url="jdbc:sqlite:c:/operator.db"

pom.xml

<modelVersion>4.0.0</modelVersion> <groupId>com.operator</groupId> <artifactId>Operator</artifactId> <version>1.00</version> <packaging>jar</packaging> <name>SQLite</name> <description>Operator</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

###試したこと
<scope>compile</scope>
を外してみましたが、同様でした。
pom.xml

<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> </dependency>

PostgreSQLでは一覧表示したので、ロジックは合っていると思います。
pom.xml

<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/operator spring.datasource.username=postgres spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver

###補足情報(言語/FW/ツール等のバージョンなど)
Windows7
sts-3.8.2
java8

sqlte-jdbc-3.8.11.2.jar
を「参照ライブラリー」へ「ビルドパス」として構成しています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

「gradle + mybatis-spring-boot-starter + SQLite3」でのコードを見つける事が出来たので、mavenへ適用する事で、SQLite3へのselectに成功しました。

小生、mybatisはこれが、初めて関わる案件なので「resultType=”map”」での返り値:

List<Map<String, Object>> result = session.selectList("SQLite3.mybatis.selectOperator");

を、spring-boot-starter-data-jpaでの

public ModelAndView index( @ModelAttribute("formModel")Operator operator, ModelAndView mav) { log.info("★★★root accessed.★★★"); mav.setViewName("index"); mav.addObject("msg","this is operator list."); mav.addObject("formModel",operator); Iterable<Operator> list = repository.findAll(); mav.addObject("operatorlist",list); return mav; }

の「Iterable<Operator> list = repository.findAll();」を、どう書き換えるのかの調査に移ります。

===== 設定 =====
pom.xml
mybatis-spring-boot-starter1.2.0
を稼動させるには
spring-boot-starter-parent1.4.3.RELEASE
が必要な様でした。

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> </dependency>

mybatis-config.xml

<configuration> <environments default="operator_id"> <environment id="operator_id"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.sqlite.JDBC"/><!-- sqliteのドライバーを設定 --> <property name="url" value="jdbc:sqlite:c:/operator.db"/><!-- dbのファイルパス --> </dataSource> </environment> </environments> <mappers> <mapper resource="operator_mapper.xml"/> </mappers> </configuration>

application.properties

spring.datasource.driverClassName=org.sqlite.JDBC spring.datasource.url=jdbc:sqlite:c:operator.db

operator_mapper.xml

<mapper namespace="SQLite3.mybatis"> <select id="selectOperator" resultType="map"> select * from operator </select> </mapper>

p.s.
小生のSTS mavenの環境では
mybatis-config.xml
application.properties
の両方に、DB接続設定を記述しないと、Spring Boot起動時にエラーとなりました。

あと、Spring Boot起動時に

WARN 5480 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.operator]' package. Please check your configuration.

が出ているので、これを解決すれば、
「「Iterable<Operator> list = repository.findAll();」を、どう書き換えるのか」
の解決になるのではないかと思っています。

投稿2017/01/12 04:33

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問