使用Leopard Jdbc
学习如何使用Leopard Jdbc。
本指南将引导您完成使用Leopard Jdbc操作MySQL。
How to complete this guide
你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤。无论哪种方式,你最终都可以得到可工作的代码。
1、配置maven依赖
在dao模块的pom.xml加入
<dependencies>
[...]
<dependency>
<groupId>io.leopard</groupId>
<artifactId>leopard-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
[...]
</dependencies>
<repositories>
<repository>
<id>leopard-snapshots</id>
<name>Leopard Snapshots</name>
<url>http://leopard.io/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
2、配置spring
site-dao/src/main/resources/applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:leopard="http://www.leopard.io/schema/leopard"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.leopard.io/schema/leopard http://www.leopard.io/schema/leopard.xsd">
<leopard:component-scan base-package="io.leopard.site" />
<leopard:jdbc id="jdbc" host="112.126.75.27" database="example" user="example" password="leopard" />
<leopard:redis id="redis" server="112.126.75.27:6311" />
</beans>
3、使用Jdbc接口
创建site-dao/src/main/java/io/leopard/site/dao/mysql/UserDaoMyqlImpl.java
package io.leopard.site.dao.mysql;
import io.leopard.data4j.jdbc.Jdbc;
import io.leopard.data4j.jdbc.builder.InsertBuilder;
import io.leopard.site.model.User;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoMyqlImpl {
@Resource
private Jdbc jdbc;
public boolean add(User user) {
InsertBuilder builder = new InsertBuilder("user");
builder.setLong("uid", user.getUid());
builder.setString("passport", user.getPassport());
builder.setString("nickname", user.getNickname());
builder.setDate("posttime", user.getPosttime());
return this.jdbc.insertForBoolean(builder);
}
public User get(long uid) {
String sql = "select * from user where uid=?";
return this.jdbc.query(sql, User.class);
}
public boolean delete(long uid) {
String sql = "delete from user where uid=?";
return this.jdbc.updateForBoolean(sql, uid);
}
}
总结
恭喜你!您已经可以在旧项目配置使用Leopard Jdbc,虽然功能比较简单,你可以在这个基础上扩展出你的业务系统,祝您好运。