搭建springmvc4 spring4 hibernate4整合框架tomcat用什么版本
发布网友
发布时间:2022-04-08 00:29
我来回答
共3个回答
懂视网
时间:2022-04-08 04:50
com.l1.spring.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public Employee get(int id){
String sql = "select id,name,email from employees where id=?";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
Employee employee = jdbcTemplate.queryForObject(sql, rowMapper,id);
return employee;
}
}
第四条使用:
/**
* 不推荐使用 JdbcDaoSupport, 而推荐直接使用 JdbcTempate 作为 Dao 类的成员变量
*/
@Repository
public class DepartmentDao extends JdbcDaoSupport{
@Autowired
public void setDataSource2(DataSource dataSource){
setDataSource(dataSource);
}
public Department get(Integer id){
String sql = "SELECT id, dept_name name FROM departments WHERE id = ?";
RowMapper<Department> rowMapper = new BeanPropertyRowMapper<>(Department.class);
return getJdbcTemplate().queryForObject(sql, rowMapper, id);
}
}
spring4-4-jdbc-02
标签:
热心网友
时间:2022-04-08 01:58
目前最好的java-web编程环境:
spring too suit + tomcat9.0 + jdk1.8.0
可以添加hibernate 4框架,只需要添加hibernate相关jar以及在xml进行相关配置既可以了
如我的spring+hibernate的viewspace-xml(不是web.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- <context:component-scan base-package="package_name"/> -->
<context:component-scan base-package="org.lee" />
<mvc:annotation-driven />
<!-- 注解实现日记记录 -->
<aop:aspectj-autoproxy />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" /> <!-- path before request -->
<property name="suffix" value=".jsp" /> <!-- suffix -->
</bean>
<!-- 国际化 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="i18n.message"></property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- 基于Session的国际化配置 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<!-- 动态语言切换 -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>
<!-- 静态资源访问 -->
<mvc:resources location="/My97DatePicker/" mapping="/My97DatePicker/**" />
<mvc:resources location="/static/lib/" mapping="/static/lib/**" />
<mvc:resources location="/static/css/" mapping="/static/css/**" />
<mvc:resources location="/static/assets/" mapping="/static/assets/**" />
<mvc:resources location="/static/js/" mapping="/static/js/**" />
<mvc:resources location="/static/js/jqprint/" mapping="/static/js/jqprint/**" />
<mvc:resources location="/static/bootstrap-3.3.5-dist/css/"
mapping="/static/bootstrap-3.3.5-dist/css/**" />
<mvc:resources location="/static/bootstrap-3.3.5-dist/js/"
mapping="/static/bootstrap-3.3.5-dist/js/**" />
<mvc:resources location="/static/bootstrap-3.3.5-dist/fonts/"
mapping="/static/bootstrap-3.3.5-dist/fonts/**" />
<!-- Bootstrap-Multiselect -->
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/dist/css/"
mapping="/static/bootstrap-multiselect-0.9.13/dist/css/**" />
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/dist/js/"
mapping="/static/bootstrap-multiselect-0.9.13/dist/js/**" />
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/dist/less/"
mapping="/static/bootstrap-multiselect-0.9.13/dist/less/**" />
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/docs/css/"
mapping="/static/bootstrap-multiselect-0.9.13/docs/css/**" />
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/docs/js/"
mapping="/static/bootstrap-multiselect-0.9.13/docs/js/**" />
<mvc:resources location="/static/bootstrap-multiselect-0.9.13/docs/less/"
mapping="/static/bootstrap-multiselect-0.9.13/docs/less/**" />
<!-- JSR 303 Validator -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource" />
</bean>
<mvc:annotation-driven validator="validator" />
<!-- 数据库配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- MySQL数据库配置 -->
<property name="url"
value="jdbc:mysql://localhost:3306/apj?useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
abstract="false" lazy-init="false" autowire="default">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!--配置Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.lee.model"></property>
<property name="hibernateProperties">
<props>
<!-- 方言 -->
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- 控制台显示SQL -->
<prop key="show_sql">true</prop>
<!-- 自动更新表结构 -->
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--配置Hibernate事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 考题管理的ajax相关配置 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- 登录权限 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login" />
<mvc:exclude-mapping path="/logout" />
<mvc:exclude-mapping path="/static" />
<bean class="org.lee.Interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
热心网友
时间:2022-04-08 03:16
sts自带的就可以了