Leopard Jdbc: Simplified Spring Jdbc and leopardjdbc
Leopard Jdbc: simplifies Spring Jdbc usage and learns how to use Leopard Jdbc in old projects.
This guide will guide you through using Leopard Jdbc to operate MySQL.
How to complete this guide
You can start from scratch and complete each step, or you can bypass the basic setup steps you are already familiar. Either way, you can finally get the code that can work.
1. Configure maven dependency
Add pom. xml in dao Module
<dependencies> [...] <dependency> <groupId>io.leopard</groupId> <artifactId>data4j-jdbc</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. Configure spring
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userDao" class="io.leopard.guides.dao.UserDao" /><bean id="jdbc" class="io.leopard.data4j.jdbc.JdbcMysqlImplFactoryBean"><property name="host" value="112.126.75.27" /><property name="database" value="example" /><property name="user" value="example" /><property name="password" value="leopard" /></bean></beans>
3. Use the Jdbc interface
Createsrc/main/java/io/leopard/guides/dao/UserDao.java
Package io. leopard. guides. dao; import io. leopard. data4j. jdbc. jdbc; import io. leopard. data4j. jdbc. builder. insertBuilder; import io. leopard. guides. model. user; import javax. annotation. resource; public class UserDao {@ Resourceprivate Jdbc;/*** Add a user. ** @ param user * @ return returns true after successful addition. An error is thrown */public boolean add (User user) {InsertBuilder builder = new InsertBuilder ("user"); builder. setLong ("uid", user. GetUid (); builder. setString ("nickname", user. getNickname (); builder. setDate ("posttime", user. getPosttime (); return this. jdbc. insertForBoolean (builder);}/*** obtain user information based on uid. ** @ param uid * @ return if the user exists, the user object is returned. If the user does not exist, null is returned. */public User get (long uid) {String SQL = "select * from user where uid =? "; Return this. jdbc. query (SQL, User. class, uid);}/*** delete user ** @ param uid * @ return returns true if the record is successfully deleted. If the record does not exist, false is returned. If an error occurs, an exception is thrown. */public boolean delete (long uid) {String SQL = "delete from user where uid =? "; Return this. jdbc. updateForBoolean (SQL, uid );}}
To learn more about Leopard functional modules, visit http://leopard.io/
Summary
Congratulations! You can use Leopard Jdbc in the old project configuration. Although the function is simple, you can expand your business system on this basis. Good luck.