新闻项目代码一(spring mvc.xml文件整合)
1.mapper文件(即数据访问层)
<?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="cn.lzh.pro.dao.UserDao">
<!-- 查询管理员语句 -->
<select id="selectAdmin" parameterType="cn.lzh.pro.entity.po.Adminpo"
resultType="cn.lzh.pro.entity.so.UserAdmin">
select * from user_admin where username =#{username}
</select>
<!-- 查询新闻列表内容 -->
<select id="selectNews" resultType="cn.lzh.pro.entity.so.Newscontent" >
select * from user_content
</select>
<!-- 查询频道列表 -->
<select id="selectChannel" resultType="cn.lzh.pro.entity.so.Channel">
select * from user_channel
</select>
<!-- 查询一条新闻 -->
<select id="selectOneNews" resultType="cn.lzh.pro.entity.so.Newscontent" >
select * from user_content where id = #{id}
</select>
<!-- 查询一个频道 -->
<select id="selectoneChannel" parameterType="String"
resultType="cn.lzh.pro.entity.so.Channel">
select * from user_channel where channelname= #{channelname}
</select>
<!-- 插入频道 -->
<insert id="insertChannel" parameterType="cn.lzh.pro.entity.po.Channelpo">
insert into user_channel(channelname) values(#{channelname})
</insert>
<!-- 插入新闻 -->
<!-- -->
<insert id="insertNews" parameterType="cn.lzh.pro.entity.so.Newscontent">
insert into user_content(id,title,url,img,createDate,count,cid,text) values(#{id},#{title},#{url},#{img},#{createDate},#{count},#{cid},#{text})
</insert>
<!-- 更改频道名字 -->
<update id="updateChannel" parameterType="cn.lzh.pro.entity.so.Channel">
update user_channel set channelname= #{channelname} where id = #{id}
</update>
<!-- 更改新闻内容 -->
<update id="updateNewsContent" parameterType="cn.lzh.pro.entity.so.Newscontent">
update user_content set text = #{text} where id = #{id}
</update>
<!-- 更改账号信息 -->
<update id="updateMessage" parameterType="cn.lzh.pro.entity.so.UserAdmin">
update user_admin set email = #{email},nickName = #{nickName},note=#{note} where id = #{id}
</update>
<!-- 更改账号密码 -->
<update id="updatePassword" parameterType="cn.lzh.pro.entity.po.Adminpo">
update user_admin set password = #{password} where username = #{username}
</update>
<!-- 删除频道 -->
<delete id="deleteChannel" parameterType="Integer">
delete from user_channel where id = #{id}
</delete>
</mapper>
2. mybatis文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration >
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 默认为true -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启 转化列命名 Table(create_time)————>entity(createTime)-->
<!--<setting name="mapUnderscoreTOCamelCase" value="true"/>-->
</settings>
</configuration>
3. spring核心文件
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--数据库连接池 -->
<bean id ="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="url" value="jdbc:mysql://localhost:3306/news" />
<property name="username" value="root"/>
<property name="password" value="010617"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!-- spring与 mybatis的结合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<!-- mybatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<!-- 扫描entity包 使用映射别名 -->
<property name="typeAliasesPackage" value="cn.lzh.pro.entity.so" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/AdminMapping.xml"/>
</bean>
<!-- dao接口所在包 通过mybatis进行查找数据库信息 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入SQL sessionDactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao的接口包 -->
<property name="basePackage" value="cn.lzh.pro.dao" />
</bean>
<!-- 事务管理器 -->
<bean id ="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds" />
</bean>
</beans>
4. spring serivce配置
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--事务器配置-->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" timeout="1000"/>
<tx:method name="query*" read-only="true" timeout="-1"/>
<tx:method name="update*" read-only="false" rollback-for="java.io.IOException"/>
<tx:method name="add*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 扫描service包下的所用注释的类型 -->
<context:component-scan base-package="cn.lzh.pro.service" />
</beans>
5. spring web配置
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--默认静态servlet -->
<mvc:default-servlet-handler/>
<!-- 进行web配置 -->
<mvc:annotation-driven />
<!-- 配置jsp文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
<!-- 配置json格式的映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters"><!-- 消息转换 -->
<list><!-- 可以写多个list 用来转换消息 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat"><!-- 对日期的一个转换 -->
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
<!-- 开启web层注解扫描 -->
<context:component-scan base-package="cn.lzh.pro.web" />
</beans>
版权声明:本文为lzh623015455原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。