使用maven创建mybatis项目
一、创建空项目
mvn archetype:generate
发现少了src/main/resources目录,于是新建一个,然后打开项目下的隐藏文件.classpath,添加
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry>
二、导入依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.32.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>
具体版本依据maven仓库的最新版本为准
三、配置mybatis
创建resources/config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite::resource:sqlite/demo.db" /> </dataSource> </environment> </environments> </configuration>
我将sqlite数据库保存在resources/sqlite目录下了
建表
create table `user` ( `id` integer primary key autoincrement, `name` text not null, `age` integer default 0 );
顺便插入几条数据
insert into user(name) values ('张三'), ('李四'), ('王五');
我选择使用mybatis的代理模式来操作数据库,创建实体类
package demo.entity; import lombok.Data; @Data public class User { private Long id; private String name; private Integer age; }
创建持久层接口
package demo.repository; import java.util.List; import demo.entity.User; public interface UserRepository { List<User> findAll(); }
创建mapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="demo.repository.UserRepository"> <select id="findAll" resultType="demo.entity.User"> select * from user </select> </mapper>
注册mapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite::resource:sqlite/demo.db" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserRepository.xml" /> </mappers> </configuration>
四、启动
修改启动类App.java
package demo; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import demo.repository.UserRepository; public class App { public static void main(String[] args) { try { InputStream inputStream = Resources.getResourceAsStream("config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserRepository userRepository = sqlSession.getMapper(UserRepository.class); System.out.println(userRepository.findAll()); } catch (IOException e) { e.printStackTrace(); } } }
也可以使用类加载器加载配置文件
InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
五、打包
mvn clean package
在META-INF/MANIFEST.MF添加
Main-Class: demo.App
运行
java -jar demo.jar