1.新建web项目,并新建各个包,引入spring框架
编写实体类以及mapper接口及mapper.xml配置文件
实例StudentMapper接口:
public interface StudentMapper {
public int save(Student st);
public int update(Student st);
public int delById(Integer sid);
public Student findById(Integer sid);
public List<Student> findPageAll(PageBean pb);
public int findMaxPage();
}
StudentMappe.xml配置文件:
id对应StudentMappe接口中的方法名 ,
parameterTyp是此方法的传参类型
在查找方法中resultType是返回类型
<?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.mapper.StudentMapper">
<insert id="save" parameterType="com.po.Student">
insert into student(sname,sex,address,birthday,classid)
values(#{sname},#{sex},#{address},#{birthday},#{classid})
</insert>
<update id="update" parameterType="com.po.Student">
update student set sname=#{sname},
sex=#{sex},
address=#{address},
birthday=#{birthday},
classid=#{classid} where sid=#{sid}
</update>
<delete id="delById" parameterType="java.lang.Integer">
delete from student where sid=#{sid}
</delete>
<select id="findById" parameterType="java.lang.Integer" resultType="com.po.Student">
select * from student where sid=#{sid}
</select>
<select id="findPageAll" parameterType="com.bean.PageBean" resultType="com.po.Student">
select s.*,c.cname from student s,clazz c where 1=1
and s.classid=c.cid
order by sid limit ${(page-1)*rows},${rows}
</select>
<select id="findMaxPage" resultType="int">
select count(*) from student where 1=1
</select>
</mapper>
2.在src目录下新建一个mybatis-config.xml的xml文件内容如下:
<?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>
<mappers>
<mapper resource="com/mapper/StudentMapper.xml" />
<mapper resource="com/mapper/ClazzMapper.xml" />
</mappers>
</configuration>
3.编写你的业务逻辑以及每个action类并在每个mapper接口注释为@Service
如图,StudentMapper
4.在编写好的业务类前加上注释@Service@Transactional
,并在声明的mapper前加上@Autowired
5.在action类前加上@Controller
注解,并在所声明的biz类前加上@Autowired
,
注意:声明的biz必须为借口式声明如:private IStudentBiz studentBiz;
在每个方法前加上@RequestMapping(value="save_Student.do")
相当于form表单中的action,return默认使用请求转发方式,若需要重定向,则在路径前加上redirect:,示例:return "redirect:fail.jsp"
6.applicationContext.xml配置文件的书写:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
"
default-autowire="byName"
>
<!-- 配置注解注入的支持 -->
<context:annotation-config></context:annotation-config>
<!-- <context:component-scan base-package="com.dao"></context:component-scan> -->
<context:component-scan base-package="com.biz"></context:component-scan>
<context:component-scan base-package="com.action"></context:component-scan>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
<property name="username" value="root"></property>
<property name="password" value="java"></property>
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 在mapper包扫描所有的sql映射文档xxxMapper.xml文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper" />
</bean>
<!-- JDBC事务管理 -->
<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txmanager"/>
</beans>
7.然后将applicationContext.xml复制一份并改名为spring-servlet.xml放在WEB-INF文件夹下,配置内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
"
default-autowire="byName"
>
<!-- 配置注解注入的支持 -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.action"></context:component-scan>
</beans>:
8.接着是web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- ******************spring容器的启动配置,替代ApplicationContext对象的创建************* -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC中文转码过滤器配置********************************* -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ************************************************
springmvc转发的servlet配置*********************** -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>springmvc</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- ************************************************** -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
9.最后新建一个名为log4j.properties的日志文件
#All level less than INFO will be logged
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
ok!.配置完成!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96886.html