新建项目
1,使用eclipse创建动态web工程,取工程名MyBatisTest。我的环境是tomcat8.5,jdk1.8
2,添加项目需要的jar包
项目配置
打开WebContent(WebRoot)/WEB-INF下的文件,配置如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns="/xml/ns/javaee"
-
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"
-
id="WebApp_ID" version="3.1">
-
<display-name>MyBatisTest</display-name>
-
<welcome-file-list>
-
<welcome-file></welcome-file>
-
</welcome-file-list>
-
-
<!-- Spring配置文件 -->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:</param-value>
-
</context-param>
-
<!-- 编码过滤器 -->
-
<filter>
-
<filter-name>encodingFilter</filter-name>
-
<filter-class></filter-class>
-
<async-supported>true</async-supported>
-
<init-param>
-
<param-name>encoding</param-name>
-
<param-value>UTF-8</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>encodingFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
<!-- Spring监听器 -->
-
<listener>
-
<listener-class></listener-class>
-
</listener>
-
-
<!-- 添加对springmvc的支持 -->
-
<servlet>
-
<servlet-name>springMVC</servlet-name>
-
<servlet-class></servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
<async-supported>true</async-supported>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>springMVC</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
</web-app>
在src目录下新建如下配置文件:
配置如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p"
-
xmlns:aop="/schema/aop" xmlns:context="/schema/context"
-
xmlns:jee="/schema/jee" xmlns:tx="/schema/tx"
-
xsi:schemaLocation="
-
/schema/aop /schema/aop/spring-aop-4.
-
/schema/beans /schema/beans/spring-beans-4.
-
/schema/context /schema/context/spring-context-4.
-
/schema/jee /schema/jee/spring-jee-4.
-
/schema/tx /schema/tx/spring-tx-4.">
-
-
<!-- 加载配置属性文件 -->
-
<context:property-placeholder
-
ignore-unresolvable="true" location="classpath:" />
-
-
<!-- 使用Annotation自动注册Bean,在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
-
<context:component-scan base-package="">
-
<context:exclude-filter type="annotation"
-
expression="" />
-
</context:component-scan>
-
-
<!-- 数据源配置, 不使用连接池 -->
-
<bean id="dataSource"
-
class="">
-
<property name="driverClassName" value="${}" />
-
<property name="url" value="${}" />
-
<property name="username" value="${}" />
-
<property name="password" value="${}" />
-
</bean>
-
-
<!-- 配置mybatis的sqlSessionFactory -->
-
<bean id="sqlSessionFactory" class="">
-
<property name="dataSource" ref="dataSource" />
-
<!-- 自动扫描文件 -->
-
<property name="mapperLocations" value="classpath:com/zby/mappers/*.xml"></property>
-
<!-- mybatis配置文件 -->
-
<property name="configLocation" value="classpath:"></property>
-
</bean>
-
-
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
-
<bean class="">
-
<property name="basePackage" value="" />
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
-
</bean>
-
-
<!-- 定义事务 -->
-
<bean id="transactionManager"
-
class="">
-
<property name="dataSource" ref="dataSource" />
-
</bean>
-
-
</beans>
配置如下:
-
=
-
=jdbc:mysql://127.0.0.1:3306/mybatis_test?serverTimezone=UTC&characterEncoding=utf8&useSSL=true
-
=root
-
=root
log4j.properties配置如下:
-
=info,appender1,appender2
-
-
.appender1=.
-
-
.appender2=.
-
.=D:/
-
-
.=.
-
.=.
配置如下:
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<!DOCTYPE configuration
-
PUBLIC "-////DTD Config 3.0//EN"
-
"/dtd/">
-
<configuration>
-
-
<settings>
-
<setting name="logImpl" value="STDOUT_LOGGING"/>
-
</settings>
-
-
<!-- 别名 -->
-
<typeAliases>
-
<package name="" />
-
</typeAliases>
-
-
</configuration>
配置如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns:p="/schema/p"
-
xmlns:aop="/schema/aop"
-
xmlns:context="/schema/context"
-
xmlns:jee="/schema/jee"
-
xmlns:tx="/schema/tx"
-
xsi:schemaLocation="
-
/schema/aop /schema/aop/spring-aop-4.
-
/schema/beans /schema/beans/spring-beans-4.
-
/schema/context /schema/context/spring-context-4.
-
/schema/jee /schema/jee/spring-jee-4.
-
/schema/tx /schema/tx/spring-tx-4.">
-
-
<!-- 使用Annotation自动注册Bean,只扫描@Controller -->
-
<context:component-scan base-package="" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
-
<context:include-filter type="annotation" expression=""/>
-
</context:component-scan>
-
-
<!-- 定义视图文件解析 -->
-
<bean class="">
-
<property name="prefix" value="/"/>
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
-
</beans>
创建数据库
使用mysql5.7,创建数据库mybatis_test,创建表t_user
-
CREATE TABLE `t_user` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`user_name` varchar(20) DEFAULT NULL,
-
`user_pwd` varchar(20) DEFAULT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB
插入数据
insert into t_user(user_name,user_pwd) values("java","1234");
编码实现
jsp文件:,
系统欢迎页面是,输入用户名和密码,若用户名密码正确,跳转至页面,错误则给出提示
内容:
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding="UTF-8"%>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>Insert title here</title>
-
</head>
-
<body>
-
<form action="${ }/user/login" method="post">
-
userName:<input type="text" name="userName" value="${ }"/><br/>
-
password:<input type="password" name="userPwd" value="${ }"><br/>
-
<input type="submit" value="login"/><font color="red">${errorMsg }</font>
-
</form>
-
</body>
-
</html>
内容如下:
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding="UTF-8"%>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>Insert title here</title>
-
</head>
-
<body>
-
欢迎:${ }
-
</body>
-
</html>
entity层:
-
package ;
-
-
public class User {
-
-
private Integer id;
-
private String userName;
-
private String userPwd;
-
-
public Integer getId() {
-
return id;
-
}
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
public String getUserName() {
-
return userName;
-
}
-
public void setUserName(String userName) {
-
this.userName = userName;
-
}
-
public String getUserPwd() {
-
return userPwd;
-
}
-
public void setUserPwd(String userPwd) {
-
this.userPwd = userPwd;
-
}
-
-
-
}
映射文件:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE mapper
-
PUBLIC "-////DTD Mapper 3.0//EN"
-
"/dtd/">
-
<mapper namespace="">
-
-
<resultMap type="User" id="UserMap">
-
<result property="id" column="id"/>
-
<result property="userName" column="user_name"/>
-
<result property="userPwd" column="user_pwd"/>
-
</resultMap>
-
-
<select id="login" parameterType="User" resultMap="UserMap">
-
select * from t_user where user_name = #{userName} and user_pwd = #{userPwd}
-
</select>
-
</mapper>
dao层:
-
package ;
-
-
import ;
-
-
public interface UserDao {
-
-
public User login(User user);
-
}
service层:
-
package ;
-
-
import ;
-
import ;
-
-
import ;
-
import ;
-
-
@Service
-
public class UserService {
-
-
@Autowired
-
private UserDao userDao;
-
-
public User login(User user) {
-
return (user);
-
}
-
}
controller层:
-
package ;
-
-
import ;
-
-
import ;
-
import ;
-
-
import ;
-
import ;
-
-
import ;
-
import ;
-
-
@Controller
-
@RequestMapping("/user")
-
public class UserController {
-
-
@Autowired
-
private UserService userService;
-
-
@RequestMapping("/login")
-
public String login(User user,HttpServletRequest request) {
-
User userResult = (user);
-
-
if (userResult == null) {
-
("user", userResult);
-
("errorMsg", "用户名或密码错误!!");
-
return "index";
-
}
-
else {
-
HttpSession httpSession = ();
-
("currentUser", userResult);
-
return "success";
-
}
-
}
-
}
启动项目
将项目添加到tomcat,启动tomcat,在浏览器输入http://localhost:8080/MyBatisTest,回车,浏览器显示如下:
输入用户名java,密码1234,点击login,系统跳转至页面
输入用户名java,密码1111,系统提示用户名或密码错误
mybatis配置文件实现控制台打印sql
配置如下红框内容即可
注意
settings必须配置在typeAliases前面,否则会提示
-
The content of element type "configuration" must match
-
"(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".
即配置项必须按照上面提示顺序配置