SSM教程 mybatis
Posted Lorentz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM教程 mybatis相关的知识,希望对你有一定的参考价值。
完整项目见 https://gitee.com/anti-murphy/demo_ssm
第一步:在resources目录下新建jdbc.properties文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/library_ssm?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=000000
第二步:在resources目录下的mybatis目录下新建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> <properties resource="jdbc.properties"/> <typeAliases> <typeAlias alias="Book" type="cn.xyz.entity.Book"/> <typeAlias alias="User" type="cn.xyz.entity.User"/> <typeAlias alias="Admin" type="cn.xyz.entity.Admin"/> <typeAlias alias="Record" type="cn.xyz.entity.Record"/> </typeAliases> <!-- 环境,可以配置多个,default:指定采用哪个环境 --> <environments default="development"> <!-- id:唯一标识 --> <environment id="development"> <!-- 事务管理器,JDBC类型的事务管理器 --> <transactionManager type="JDBC" /> <!-- 数据源,池类型的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <!--<mapper resource="cn/xyz/dao/mapper/BookMapper.xml"/>--> <package name="cn.xyz.dao.mapper"/> </mappers> </configuration>
第三步:在entity目录下建立需要的dao
package cn.com.entity;
import org.springframework.stereotype.Component;
@Component
public class User {
private int id;
private String name;
private String psd;
private int level;
public User() {
}
public User(String name, String psd) {
this.name = name;
this.psd = psd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name=\'" + name + \'\\\'\' +
", psd=\'" + psd + \'\\\'\' +
", level=" + level +
\'}\';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
第四步:在java.cn.com.dao.mapper下新建 接口UserMapper.java
public interface UserMapper { public List<User> queryUserAll(); public User queryUserById(int id); public int insertUser(User user); public void deleteUser(int id); public void updateUser(User user); }
第五步:在resources.cn.com.dao.mapper下新建接口对应的xml 如UserMapper.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"> <mapper namespace="cn.xyz.dao.mapper.UserMapper"> <select id="queryUserAll" resultType="User"> select * from user </select> <select id="queryUserById" parameterType="int" resultType="User"> select * from user where id = #{id} </select> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="User"> insert into user(name,psd) value (#{name},#{psd}) </insert> <delete id="deleteUser" parameterType="int"> delete from user where id = #{id} </delete> <update id="updateUser" parameterType="User"> update user set <if test="name!=null and name!=\'\'"> name = #{name}, </if> <if test="psd!=null and psd!=\'\'"> psd=#{psd}, </if> <if test="level!=null"> level=#{level}, </if> id=#{id} where id = #{id} </update> </mapper>
第六步:在test目录下新建测试文件
package cn.xyz.dao.mapper; import cn.xyz.entity.User; import cn.xyz.util.SessionFactory; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import javax.annotation.Resource; import java.util.Iterator; import java.util.List; import static org.junit.Assert.*; public class UserMapperTest { static Logger logger = Logger.getLogger(UserMapperTest.class); @Test public void queryUserAll() { try (SqlSession session = SessionFactory.getSession()){ UserMapper userMapper = session.getMapper(UserMapper.class); List<User> users = userMapper.queryUserAll(); Iterator<User> it = users.iterator(); while(it.hasNext()){ User user = it.next(); logger.info(user.toString()); } } }
以上是关于SSM教程 mybatis的主要内容,如果未能解决你的问题,请参考以下文章
[转]SSM(Spring+SpringMVC+Mybatis)框架搭建详细教程附源代码Demo
SSM框架——使用MyBatis Generator自动创建代码
java高并发框架 SSM框架 详细整合教程(Spring+SpringMVC+MyBatis)
免费下载全套最新010-3Spring springmvc mybatis整合项目视频教程+教学资料+学习课件+源代码+软件开发工具