赞
踩
jpa
性能确实差,转战 mybatis
了
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.15</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>4.0.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency>
@Data
public class UserEntity {
private Long id;
private String username;
private String password;
private String nickname;
private Date createTime;
private Date updateTime;
}
public interface UserMapper {
List<UserEntity> selectAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.lingkang.mmt.mapper.UserMapper">
<select id="selectAll" resultType="top.lingkang.mmt.entity.UserEntity">
select * from t_user
</select>
</mapper>
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.io.Resources; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import top.lingkang.mmt.mapper.UserMapper; import java.io.InputStream; /** * @author lingkang * Created by 2024/2/28 */ public class Demo01 { public static void main(String[] args) throws Exception { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=UTC"); config.setUsername("root"); config.setPassword("123456"); HikariDataSource dataSource = new HikariDataSource(config); Configuration configuration = new Configuration(); // 用for循环加载所有 xml InputStream inputStream = Resources.getResourceAsStream("mapper/UserMapper.xml"); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, "mapper/UserMapper.xml", configuration.getSqlFragments()); mapperParser.parse(); // 配置 TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("dev", transactionFactory, dataSource); configuration.setEnvironment(environment); configuration.setMapUnderscoreToCamelCase(true);// 下划线转化驼峰 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); // 查询 SqlSession session = sqlSessionFactory.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); System.out.println(mapper.selectAll());// 查询所有 session.close(); } }
转自:https://lingkang.top/archives/tong-guo-dai-ma-jia-zai-mybatis-de-mapperxml
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。