public class User
{
private int id;
private String name;
private int age;
public User(int id, String name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Mapper
public interface UserMapper
{
@Select("select * from user where id = #{id}")
public User findUserByID(@Param("id") int id);
@Insert("insert into user(name, age) values(#{name}, #{age})")
public void insertUser(User user);
}
application.yml
server:
port: 8088
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
------------------------------------------------------------------
------------------------------------------------------------------
public class User
{
private int id;
private String name;
private int age;
public User(int id, String name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Mapper
public interface UserMapper
{
public User findUserByID(int id);
public void insertUser(User user);
}
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="com.springboot.test.mapper.UserMapper">
<select id="findUserByID" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
<insert id="insertUser" parameterType="User">
insert into user(name, age) values(#{name}, #{age})
</insert>
</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>
<typeAliases>
<package name="com.springboot.test.domain"/>
</typeAliases>
</configuration>
application.xml
server:
port: 8088
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
config-location: classpath:/mybatis-config.xml