sqlite整合spring,springmvc
来源:互联网 发布:美国法学院top14知乎 编辑:程序博客网 时间:2024/06/06 13:10
因为需要用到轻量级的数据库,所以使用了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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aophttp://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. */@Repositorypublic 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框架中编写一样,也就不在编写代码了。主要层的代码已经粘贴, 以后有需要的可以参考下
阅读全文
0 0
- sqlite整合spring,springmvc
- SpringMVC-整合Spring整合
- SpringMVC-15 Spring整合SpringMVC
- SpringMVC+Spring+Ibatis整合
- SpringMVC+Spring+Hibernate整合
- SpringMVC + Spring + SpringJDBC整合
- SpringMVC整合Spring、Hibernate
- SpringMVC + Spring + SpringJDBC整合
- SpringMVC + Spring + SpringJDBC整合
- SpringMVC + Spring + SpringJDBC整合
- Spring-SpringMVC-Hibernate整合
- Spring-SpringMVC-Mybatis整合
- springmvc+spring+mybatis整合
- SpringMVC+spring+maven整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- springmvc+spring+mybatis 整合
- 计算两向量的欧式距离,余弦相似度
- The CATALINA_HOME environment variable is not defined correctly
- 类前面有红叉
- [1]java微信开发-准备工作
- wechat and alipay QRCode
- sqlite整合spring,springmvc
- MyEclipse 常用报错
- 协同推荐系统简介
- retrofit笔记
- springboot reids 对象转换异常
- 香水美容化妆品公司织梦dedecms模板(带手机端)
- 为RecyclerView添加Header和Footer,支持三种LayoutManager
- 数组单调和
- ASP.NET URL重写(伪静态)