第一步:创建java项目
第二步:导入依赖包
第三步:配置db.properties
jdbc. driverClassName= com. mysql. jdbc. Driver
jdbc. url= jdbc: mysql: / / localhost: 3306 / gj1
jdbc. username= root
jdbc. password= 123456
jdbc. maxActive= 10
第四步:配置log4j.properties
# Global logging configuration
log4j. rootLogger= ERROR, stdout
# MyBatis logging configuration. . .
# 配置日志输入 : log4j. logger. 映射文件的包 = 日志级别5 个(error,warin,info,debug,all/ trace)
log4j. logger. cn. zj. ssm. mapper= all
# Console output. . .
log4j. appender. stdout= org. apache. log4j. ConsoleAppender
log4j. appender. stdout. layout= org. apache. log4j. PatternLayout
log4j. appender. stdout. layout. ConversionPattern= % 5 p [ % t] - % m% n
第五步:配置SpringConfig
package cn. zj. ssm. config;
import javax. sql. DataSource;
import org. apache. ibatis. session. SqlSessionFactory;
import org. mybatis. spring. SqlSessionFactoryBean;
import org. mybatis. spring. annotation. MapperScan;
import org. springframework. beans. factory. annotation. Value;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. ComponentScan;
import org. springframework. context. annotation. Configuration;
import org. springframework. context. annotation. PropertySource;
import org. springframework. context. annotation. Scope;
import org. springframework. jdbc. core. JdbcTemplate;
import org. springframework. jdbc. datasource. DataSourceTransactionManager;
import org. springframework. transaction. PlatformTransactionManager;
import org. springframework. transaction. annotation. EnableTransactionManagement;
import com. alibaba. druid. pool. DruidDataSource;
@Configuration
@ComponentScan ( "cn.zj.ssm" )
@PropertySource ( "classpath:db.properties" )
@EnableTransactionManagement
@MapperScan ( "cn.zj.ssm.mapper" )
public class SpringConfig {
@Value ( "${jdbc.driverClassName}" )
private String driverClassName;
@Value ( "${jdbc.url}" )
private String url;
@Value ( "${jdbc.username}" )
private String username;
@Value ( "${jdbc.password}" )
private String password;
@Value ( "10" )
private Integer maxActive;
@Bean ( initMethod = "init" , destroyMethod = "close" )
public DataSource getDataSource ( ) {
System. out. println ( "创建数据源" ) ;
DruidDataSource dataSouce = new DruidDataSource ( ) ;
dataSouce. setDriverClassName ( this . driverClassName) ;
dataSouce. setUrl ( this . url) ;
dataSouce. setUsername ( this . username) ;
dataSouce. setPassword ( this . password) ;
dataSouce. setMaxActive ( this . maxActive) ;
return dataSouce;
}
@Bean
public SqlSessionFactory getSqlSessionFactory ( ) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean ( ) ;
bean. setDataSource ( getDataSource ( ) ) ;
bean. setTypeAliasesPackage ( "cn.zj.ssm.pojo" ) ;
return bean. getObject ( ) ;
}
@Bean
public PlatformTransactionManager getTxManager ( ) {
return new DataSourceTransactionManager ( getDataSource ( ) ) ;
}
}
第六步:配置pojo
package cn. zj. ssm. pojo;
public class User {
private Integer id;
private String name;
private String phone;
private String password;
private Integer age;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getPhone ( ) {
return phone;
}
public void setPhone ( String phone) {
this . phone = phone;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
@Override
public String toString ( ) {
return "User [id=" + id + ", name=" + name + ", phone=" + phone + ", password=" + password + ", age=" + age
+ "]" ;
}
}
第七步:配置UserMapper.java
package cn. zj. ssm. mapper;
import java. util. List;
import cn. zj. ssm. pojo. User;
public interface UserMapper {
@Select ( "select * from user where id = #{id}" )
public User selectByPrimaryKey ( Integer id) ;
@Select ( "select * from user" )
public List< User> selectList ( ) ;
@Insert ( "insert into user(name,age,password,phone)values(#{name},#{age},#{password},#{phone})" )
public int insert ( User user) ;
@Update ( "update user set name = #{name},age=#{age},phone=#{phone},password=#{password} where id = #{id}" )
public int updateByPrimaryKey ( User user) ;
@Delete ( "delete from user where id = #{id}" )
public int deleteByPrimaryKey ( Integer id) ;
}
第八步:配置UserServiceImpl
package cn. zj. ssm. service. impl;
import java. util. List;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import cn. zj. ssm. mapper. UserMapper;
import cn. zj. ssm. pojo. User;
import cn. zj. ssm. service. UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectByPrimaryKey ( Integer id) {
return userMapper. selectByPrimaryKey ( id) ;
}
@Override
public List< User> selectList ( ) {
return userMapper. selectList ( ) ;
}
@Override
public int insert ( User user) {
return userMapper. insert ( user) ;
}
@Override
public int updateByPrimaryKey ( User user) {
return userMapper. updateByPrimaryKey ( user) ;
}
@Override
public int deleteByPrimaryKey ( Integer id) {
return userMapper. deleteByPrimaryKey ( id) ;
}
}
第九步:配置测试类
package cn. zj. ssm. test;
import static org. junit. Assert. *;
import java. util. List;
import org. junit. Test;
import org. junit. runner. RunWith;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. test. context. ContextConfiguration;
import org. springframework. test. context. junit4. SpringJUnit4ClassRunner;
import cn. zj. ssm. pojo. User;
import cn. zj. ssm. service. UserService;
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSelectByPrimaryKey ( ) {
User user = userService. selectByPrimaryKey ( 4 ) ;
System. out. println ( user) ;
}
@Test
public void testSelectList ( ) {
List< User> users = userService. selectList ( ) ;
}
@Test
public void testInsert ( ) {
}
@Test
public void testUpdateByPrimaryKey ( ) {
}
@Test
public void testDeleteByPrimaryKey ( ) {
}
}