创建一个WEB工程发布到TomCat中
一,下载各种工具
1,下载jdk
2,下载Tomcat
3,下载maven
4,下载mysql
二,搭建环境
1,
2,配置mybatis
*,创建一个user-mapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.informationmanagerment.mapper.user-mapper">
<!--根据id来查询user -->
<select id="selectUserById" parameterType="int" resultType="com.informationmanagerment.vo.User">
select * from user where id=#{id}
</select>
<select id="selectUser" parameterType="hashmap" resultType="com.informationmanagerment.vo.User">
select * from user where name=#{name} and password=#{password}
</select>
<select id="addUser" parameterType="com.informationmanagerment.vo.User">
insert into user (name,password,sex,age) VALUES (#{name},#{password},#{sex},#{age})
</select>
<select id="selectAllUser" resultType="com.informationmanagerment.vo.User">
select * from user
</select>
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
*,创建一个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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<typeAliases>
<typeAlias type="com.informationmanagerment.vo.User" alias="User"/>
</typeAliases>
<environments default="local">
<environment id="local">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/informationmanagerment?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="mysqladmin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/informationmanagerment/mapper/user-mapper.xml"/>
</mappers>
</configuration>
*,写一个MySqlSession类//用来获取sqlsession
package com.informationmanagerment.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MySqlSession {
private static String mybatisResource = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getSqlSessionFactory() {
try {
f (sqlSessionFactory == null) {
InputStream inputStream = Resources.getResourceAsStream(mybatisResource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
return sqlSessionFactory;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession newSqlSession() {
SqlSession session = getSqlSessionFactory().openSession();
return session;
}
}
*在dao层中使用mybatis来查询数据
SqlSession session = MySqlSession.newSqlSession();
User user=null;
Map<String, String> hash=new HashMap<>();
hash.put("name", name);
hash.put("password", password);
try {
user = session.selectOne("selectUser", hash);
} catch (Exception e) {
e.printStackTrace();
}finally {
session.close();
}