标签:spring jdbc java mysql leopard
本指南将引导您完成使用Leopard Jdbc操作MySQL。
你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤。无论哪种方式,你最终都可以得到可工作的代码。
在dao模块的pom.xml加入
<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>
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>
创建src/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 { @Resource private Jdbc jdbc; /** * 添加用户. * * @param user * @return 添加成功返回true,出错抛异常 */ 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); } /** * 根据uid获取用户信息. * * @param uid * @return 用户存在则返回用户对象,不存在则返回null. */ public User get(long uid) { String sql = "select * from user where uid=?"; return this.jdbc.query(sql, User.class, uid); } /** * 删除用户 * * @param uid * @return 成功删除记录就返回true,记录不存在则返回false,出错则抛异常. */ public boolean delete(long uid) { String sql = "delete from user where uid=?"; return this.jdbc.updateForBoolean(sql, uid); } }
了解Leopard更多功能模块,请访问http://leopard.io/
恭喜你!您已经可以在旧项目配置使用Leopard Jdbc,虽然功能比较简单,你可以在这个基础上扩展出你的业务系统,祝您好运。
标签:spring jdbc java mysql leopard
原文地址:http://blog.csdn.net/tanhaichao/article/details/43727885