sqlite整合spring,springmvc

本文介绍了如何将SQLite数据库整合到Spring和SpringMVC项目中,包括下载sqlite-jdbc驱动,添加到lib目录,配置pom.xml的plugin,以及applicationContext的XML配置。最后提到了 Dao接口的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为需要用到轻量级的数据库,所以使用了sqlite,按照网上给的例子,自己整合了一下原本的ssm项目,现在把项目整合后的一些代码写出来,供以后使用

先看下项目目录


首先要注意的是下载sqlite-jdbc-3.8.11.2.jar这样的架包, 使用maven配置的那个jar包,在项目中无法引用,所以必须下载下来,然后放在lib中

然后是配置的pom.xml中需要在build中添加一个plugin插件

<build>  
    <plugins>  
      <plugin>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <configuration>  
          <source>1.5</source>  
          <target>1.5</target>  
          <encoding>UTF-8</encoding>  
               <compilerArguments>  
                   <extdirs>${basedir}/WebRoot/WEB-INF/lib</extdirs>  
               </compilerArguments>  
        </configuration>  
      </plugin>  
    </plugins>  
  </build>

这样就可以引用本地添加的jar包了。

 下面是applicationContext-sqlite.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


    <!-- 定义数据源Bean-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClassName" value="org.sqlite.JDBC" />
        <!-- 指定连接数据库的URL -->
        <property name="url" value="jdbc:sqlite:E:/JDBC/jmtapp.db" />
    </bean>


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>

    <!-- 配置对应的dao 接口实现类bean
        其中ID=“hzDao” ,这个需要和对应的dao接口实现类相对应
     -->
    <bean id="hzDao" class="com.jmtapp.jmth5.mapper.impl.hzDaoImpl">
        <property name="jdbcTemplate">
            <ref local="jdbcTemplate" />
        </property>
    </bean>



</beans>

然后是web.xml里面需要添加对应的applicationContext-sqlite.xml地址,要不,无法进行调用


<servlet>
    <servlet-name>jmth5</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/conf/applicationContext.xml,classpath:conf/applicationContext-sqlite.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>


这些配置好后,就可以写sqlUtil工具类了。

package com.jmtapp.jmth5.mapper;

import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * sqlite的工具类
 * Created by T430 on 2017/7/5.
 */
@Repository
public class sqliteUtil {

    /**
     * 数据源
     */
    @Resource
    private  DataSource sqliteDataSource;

    /**
     * 获取数据库连接
     * @return conn
     */
    public  Connection getConnection() throws SQLException {
        Connection conn = sqliteDataSource.getConnection();
        conn.setAutoCommit(false);
        return conn;
    }

    /**
     * 关闭数据库连接
     * @param conn
     */
    public  void close(Connection conn, Statement stmt, ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException ex) {

            }
            rs = null;
        }
        if (null != stmt) {
            try {
                stmt.close();
            } catch (SQLException ex) {

            }
            stmt = null;
        }
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException ex) {

            }
            rs = null;
        }
    }
}

需要注意的是,因为调用了@Respiratory,所以这个工具类需要放到Dao的所在目录下,这样才能被spring扫描到,否则无法使用


然后是配置Dao接口

package com.jmtapp.jmth5.mapper;

import com.jmtapp.jmth5.pojo.hz_index;

import java.util.List;

/**
 * Created by T430 on 2017/7/5.
 */

public interface hZDao {

    public List<hz_index> getAllParent();//获取所有父类信息以及该父类下的第一级子类集合
    public hz_index getChildInfo(Integer id);//通过父类编号获取该父类下的户政信息

}

其实现层

package com.jmtapp.jmth5.mapper.impl;


import com.jmtapp.jmth5.mapper.hZDao;
import com.jmtapp.jmth5.pojo.hz_index;
import com.jmtapp.jmth5.mapper.sqliteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by T430 on 2017/7/5.
 */
@Repository(value = "hzDao")
public class hzDaoImpl implements hZDao {

    @Autowired
   private sqliteUtil sUtil;

    private JdbcTemplate jdbcTemplate;

    Connection conn=null;
    Statement stmt =null;
    ResultSet rs=null;

    /**
     * 获取所有的父类业务的集合
     * 其中每个父类里包含第一级子类的集合
     * @return
     */
    public List<hz_index> getAllParent() {
      //  List<hz_index> li=new ArrayList<hz_index>();
        List<hz_index> hzIndexList=null;


        try {
            conn=sUtil.getConnection();//链接数据库

             stmt =conn.createStatement();//创建数据库
            String sql="select * from hz_index where parent='0'";//编写sql语句

            //查询
             rs= stmt.executeQuery(sql);
            hz_index hz=null;
            hzIndexList=new ArrayList<>();//户政集合

         while (rs.next()){
           // System.out.println("名字:==="+rs.getString("name"));
             hz=new hz_index();
             hz.setId(rs.getInt("id"));;
             hz.setName(rs.getString("name"));

             hzIndexList.add(hz);
         }
         //新建另一个实体
            hz_index hz2=null;

            for (hz_index hh: hzIndexList
                 ) {
               // System.out.println("编号:==="+hh.getId());

                String sql2= "select * from  hz_index where parent= '"+hh.getId()+"'";

                //查询子类
                ResultSet rs2=stmt.executeQuery(sql2);
                List<hz_index> hzIndexList2=new ArrayList<>();
                //遍历
                while(rs2.next()){
                    hz2=new hz_index();
                    hz2.setId(rs2.getInt("id"));
                    hz2.setName(rs2.getString("name"));

                    hzIndexList2.add(hz2);
                    hh.setChildren(hzIndexList2);//把查询到的子类集合放到  户政的子类集合中
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            sUtil.close(conn,stmt,rs);//关闭数据库
        }
        return hzIndexList;
    }


    /**
     * 通过父类ID,获取该父类下所有的子类信息
     * @param id
     * @return
     */
    public hz_index getChildInfo(Integer id){
        hz_index hz= new hz_index();
        try {
            conn=sUtil.getConnection();

            stmt =conn.createStatement();

            String sql="select * from hz_index where parent ='"+id+"'";

            rs=stmt.executeQuery(sql);

            while (rs.next()){
                hz.setId(rs.getInt("id"));
                hz.setName(rs.getString("name"));
                hz.setValue(rs.getString("value"));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            sUtil.close(conn,stmt,rs);
        }
        return hz;
    }







    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
}

其Service层和实现层,还有Controller层都和原来ssm框架中编写一样,也就不在编写代码了。主要层的代码已经粘贴, 以后有需要的可以参考下








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值