日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc 配置和spring配置?

發布時間:2024/9/20 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc 配置和spring配置? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在接觸mybatis,之間使用springmvc時,配置文件一直是,web.xml+XX-servlet.xml 的配置(xx為web.xml中servlet name名稱)。
為了整合mybatie,各種百度,發現網上很多人說的springmvc也需要配置applicationContext.xml,據我淺薄的了解,applicationContext是spring里的配置吧。所以我想問下springmvc和spring的配置區別,還有,單就springmvc和mybatis結合使用而言,配置文件究竟怎么配置。ps:目前使用的是stringbuffer形式的拼接sql,結合org.springframework.jdbc中的nameparameterjdbctemplate來使用的,想換換新的使用,望各位不吝賜教,感謝)

====================================================================================

作者:二流程序猿
鏈接:https://www.zhihu.com/question/47565214/answer/136096996
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

springMVC負責spring控制層的處理,而servlet.xml配置文件,主要負責MVC這部分的配置,如視圖解析、上下文處理等,這里要注意的是,此文件的名稱與位置是允許在web.xml的servlet配置中定義的(init-param):

<servlet><servlet-name>graduation</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-servlet.xml</param-value></init-param><load-on-startup>2</load-on-startup><!--表示啟動容器時候初始化--></servlet><servlet-mapping><servlet-name>graduation</servlet-name><url-pattern>/</url-pattern><!--表示對所有后綴為do的請求做spring攔截--></servlet-mapping> 而application.xml用來配置spring的全局屬性,例如datasource、aop等,首先要在web.xml中添加配置(classpath:applicationContext.xml的路徑): <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-applicationContext.xml</param-value> </context-param>
下面就是如何將spring與mybatis相結合了
在applicationContext.xml這個配置文件中,我們要先配置我們的數據源:
具體配置以項目為準,這里用的是阿里的druid。 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><!--驅動名稱--><property name="driverClassName" value="${jdbc.driverClassName}"/><!--JDBC連接串--><property name="url" value="${jdbc.url}"/><!--數據庫名稱--><property name="username" value="${jdbc.username}"/><!--數據庫密碼--><property name="password" value="${jdbc.password}"/><!--初始化大小--><property name="initialSize" value="15"/><!--連接池最大使用數量--><property name="maxActive" value="20"/><!--連接池最小空閑--><property name="minIdle" value="0"/><!--配置獲取連接等待超時時間--><property name="maxWait" value="60000"/><!--配置間隔多久才進行一次檢測 , 檢測需要關閉的空閑連接--><property name="timeBetweenEvictionRunsMillis" value="60000"/><!--配置一個連接在池中最小生存時間--><property name="minEvictableIdleTimeMillis" value="300000"/><!--連接空閑時測試是否有效--><property name="testWhileIdle" value="false"/><!--獲取連接時測試是否有效--><property name="testOnBorrow" value="false"/><!--歸還連接時測試是否有效--><property name="testOnReturn" value="false"/><!--打開PSCache , 并指定每個連接上PSCache的大小--><property name="poolPreparedStatements" value="false"/><property name="maxPoolPreparedStatementPerConnectionSize" value="20"/></bean>
下面同樣在applicationContext.xml這個配置文件中,添加mybaits的配置(包括事物): <!--mybatis sessionFaction 實例--> <bean id="sqlSessionFaction" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><!--mapper.xml 映射--><property name="mapperLocations" value="classpath:mapper/*.xml"/><!--pojo映射 , 這里映射到POJO包--><property name="typeAliasesPackage" value="com.graduation.pojo"/> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.graduation.dao" /> </bean> <!--mybatis 事物配置--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /> </bean> <!-- 事務注解驅動,標注@Transactional的類和方法將具有事務性 --> <tx:annotation-driven transaction-manager="txManager" />

下面就是如何使用了
mybaits必不可少的mapper.xml、dao、pojo類。這里如何自己去編寫這些內容樓主應該可以搞定 , 要注意的是路徑要與上述配置路徑匹配。同樣,也可以借助genertor等去自動生成這些文件。
可參考:使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件(轉) - 斗爺 - 博客園
同樣我們在做交互操作時候也非常簡單,沒有必要對sessionFactory的生命周期負責了,spring全權負責。

下面是我通過generator生成的測試demo:pojo: public class Test { private Long id;private String test;public Long getId() { return id;}public void setId(Long id) { this.id = id;}public String getTest() { return test;}public void setTest(String test) { this.test = test == null ? null : test.trim();}} dao: // 這是我們Test類的dao層//這是spring的注解,有了它我們就可以通過spring的@Autowired實例化該類 @Repositorypublic interface TestDao {// 下面為CRUD操作int deleteByPrimaryKey(Long id); int insert(Test record); int insertSelective(Test record); Test selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(Test record); int updateByPrimaryKey(Test record); } mapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.graduation.dao.TestMapper" ><resultMap id="BaseResultMap" type="com.graduation.domain.Test" ><id column="id" property="id" jdbcType="BIGINT" /><result column="test" property="test" jdbcType="VARCHAR" /></resultMap><sql id="Base_Column_List" >id, test </sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >select <include refid="Base_Column_List" />from testwhere id = #{id,jdbcType=BIGINT} </select><delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >delete from testwhere id = #{id,jdbcType=BIGINT} </delete><insert id="insert" parameterType="com.graduation.domain.Test" >insert into test (id, test)values (#{id,jdbcType=BIGINT}, #{test,jdbcType=VARCHAR}) </insert><insert id="insertSelective" parameterType="com.graduation.domain.Test" >insert into test <trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id, </if><if test="test != null" >test, </if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=BIGINT}, </if><if test="test != null" >#{test,jdbcType=VARCHAR}, </if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.graduation.domain.Test" >update test <set ><if test="test != null" >test = #{test,jdbcType=VARCHAR}, </if></set>where id = #{id,jdbcType=BIGINT} </update><update id="updateByPrimaryKey" parameterType="com.graduation.domain.Test" >update testset test = #{test,jdbcType=VARCHAR}where id = #{id,jdbcType=BIGINT} </update> </mapper>
然后我們通過測試類運行這個demo(這里不能通過main測試): // 測試時候不能在main里面執行,因為main方法不會讀取spring的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring-applicationContext.xml"}) public class tets {@Autowiredprivate TestDao testDao; // 這是我們的dao 不要被名字迷惑...@Testpublic void testSearchallUser() {Test test = Test();test.setTest("123");testDao.insert(test) ;} } 這樣就OK了 !

總結

以上是生活随笔為你收集整理的springmvc 配置和spring配置?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。