目录
1.maven的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qf.j2205</groupId>
<artifactId>mybatis_sql</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis_sql Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
依赖
</dependencies>
<build>
插件
</build>
</project>
2.mybatis-config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis2"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/FilmDao.xml"/>
</mappers>
</configuration>
3.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">
<!-- namespace是dao层对应的接口类的路径 -->
<mapper namespace="com.qf.j2205.dao.FilmDao">
<!-- id自定义 type 对应实体类 -->
<resultMap id="filmInFoMap" type="com.qf.j2205.entity.FilmInfo">
<!--id 主键 property 实体类中对应的属性 column 数据库中对应的字段名-->
<id property="filmID" column="filmID" />
<result property="filmName" column="filmName" />
<result property="actor" column="actor"/>
<result property="director" column="director"/>
<result property="ticketPrice" column="ticketPrice"/>
<!--联表查询 中实体类中含有另外一个表的实体属性-->
<association property="filmType" column="typeID" javaType="com.qf.j2205.entity.FilmType">
<id property="typeID" column="typeID"/>
<result property="typeName" column="typeName"/>
</association>
</resultMap>
<select id="selectAll" resultMap="filmInFoMap">
select
f.filmID ,
f.typeID ,
f.filmName ,
f.actor,
f.director,
f.ticketPrice,
t.typeID,
t.typeName
from filminfo f , filmtype t where f.typeID = t.typeID
</select>
<select id="selectPrice" resultMap="filmInFoMap">
select
f.filmID ,
f.typeID ,
f.filmName ,
f.actor,
f.director,
f.ticketPrice,
t.typeID,
t.typeName
from filminfo f , filmtype t where f.typeID = t.typeID
and ticketPrice between #{start} and #{end }
</select>
<sql id="sqlFind" >
f.filmID ,
f.typeID ,
f.filmName ,
f.actor,
f.director,
f.ticketPrice,
t.typeName
</sql>
<select id="findById" resultType="com.qf.j2205.entity.FilmInFo2">
select
<include refid="sqlFind"/>
from filminfo f , filmtype t where f.typeID = t.typeID and f.filmID = #{id}
</select>
<insert id="addFilmInfo" parameterType="com.qf.j2205.entity.FilmInfo">
insert into filminfo values (#{filmID} , #{typeID} , #{filmName} , #{actor} , #{director} , #{ticketPrice})
</insert>
<delete id="deleteById" parameterType="int">
delete from filminfo where filmID = #{ids}
</delete>
<select id="selectByFilm" parameterType="com.qf.j2205.entity.FilmInfo" resultType="com.qf.j2205.entity.FilmInfo">
select * from filminfo
-- where 会自动忽略前后缀(and | or)
-- 注意: test 里边的是对应类型的默认值
<where>
<if test="filmID != 0">
filmID = #{filmID}
</if>
<if test="typeID != 0">
and typeID = #{typeID}
</if>
<if test="filmName != null">
and filmName = #{filmName}
</if>
<if test="actor != null">
and actor = #{actor}
</if>
<if test="director != null">
and director = #{director}
</if>
<if test="ticketPrice != 0">
and ticketPrice = #{ticketPrice}
</if>
</where>
</select>
<update id="updates" parameterType="com.qf.j2205.entity.FilmInfo">
update filminfo
<set>
<if test="typeID != 0">
typeID = #{typeID} ,
</if>
<if test="filmName != null">
filmName = #{filmName} ,
</if>
<if test="actor != null">
actor = #{actor} ,
</if>
<if test="director != null">
director = #{director} ,
</if>
<if test="ticketPrice != 0">
ticketPrice = #{ticketPrice},
</if>
</set>
where filmID = #{filmID}
</update>
</mapper>
4.web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--注册servlet-->
<servlet>
<servlet-name>自定义名字</servlet-name>
<servlet-class>对应servlet的路径</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>自定义名字</servlet-name>
<url-pattern>/自定义路径</url-pattern>
</servlet-mapping>
</web-app>
5.JSP + jstl + el
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<%
java 代码
%>
<%= 输出Java代码 %>
${el表达式}
<%--jstl 表达式--%>
<c:forEach var="自定义变量" items="${传递过来的数据}">
</c:forEach>
<h2>Hello World!</h2>
</body>
</html>